引自免费微信小程序:皆我百晓生
在 TensorFlow 中,如果你试图关闭 tf.enable_onednn_opts 变量的环境变量设置,可能会遇到以下问题:
- TF 使用了 On-DNN 运算(oneDNN),这是一类基于全连接层进行并行计算的技术,通常用于解决大规模数据集上的特征表示、模型训练等任务。
- 设置
tf.enable_onednn_opts 变量的目的是为了开启或者关闭支持在多个 CPU 映射上并行执行 OneDNN 的选项。默认情况下,它将打开,允许 ONNX 模型在多核处理器上实现并行计算。 - 当
tf.enable_onednn_opts 被设置为零时,TensorFlow 将不会启用在单个 CPU 映射上并行执行 On-DNN 操作,这会导致性能下降。这是因为在一个 CPU 上运行一个完整 OneDNN 模型可能涉及到许多复杂的操作和递归调用,这些操作通常无法在单个CPU上同时完成。
当您想要在图形化显示中仅显示某个属性(如 'CRIN' 或 'TAX')与房价之间的关系时,通常会通过以下几个步骤来处理:
- 获取数据:首先,从 Boston Housing 数据集中加载数据集。您可以通过以下代码获取这些数据:
boston_dataset = tf.keras.datasets.boston_housing
(train_x, train_y), (_ unseen_train_x, _ unseen_train_y), _ unseen_test_x, _ unseen_test_y = boston_dataset.load_data(test_split = 0)
- 分割数据:将
train 和 test 列表分开,并分别处理它们的特征向量。
train_x = train_x.reshape((len(train_x), -1))
train_y = train_y.astype(np.float32)
unseen_train_x = unseen_train_x.reshape((len(unseen_train_x), -1))
unseen_train_y = unseen_train_y.astype(np.float32)
test_x = test_x.reshape((len(test_x), -1))
test_y = test_y.astype(np.float32)
unseen_test_x = unseen_test_x.reshape((len(unseen_test_x), -1))
unseen_test_y = unseen_test_y.astype(np.float32)
- 提取特徵:在对
train 和 unseen_train 列表进行分块之前,我们需要获取各自列的特征值。例如,在 boston_dataset 中,特征名为 'CRIM', 'ZN', 'INDUS', 'chas', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B-1000', 'LSTAT'。您可以根据您的具体需求提取这些特征,如直方图、箱线图、热力图等。
# 假设我们只关心 CRIM 特征
crim_values_train = train_x[0]
crim_values_unseen = unseen_train_x[0]
# 假设我们只关心 ZN 特征
zn_values_train = train_x[1]
zn_values_unseen = unseen_train_x[1]
# ... (根据实际情况添加其他特征提取代码)
# 统计各特征的权重
feature_weights_train = np.mean(crim_values_train)
feature_weights_unseen = np.mean(zn_values_unseen)
# 计算房价相关系数
price_corr_matrix = feature_weights_train * unseen_train_y.T / feature_weights_unseen.T
price_corr_matrix[:10, :10]
- 绘制图形:接下来,我们可以使用 matplotlib 库创建一个包含 'CRIN' 和 'TAX' 属性的柱状图,显示不同属性与房价的相关性。
import matplotlib.pyplot as plt
# 创建柱状图实例
fig, ax = plt.subplots()
# 定义柱状图布局
bar_width = 0.85
bottom_padding = 0.2
left_padding = 0.2
right_padding = 0.2
top_padding = 0.2
# 绘制柱状图
ax.barh(range(len(crim_values_train)), [price_corr_matrix[i] for i in range(len(crim_values_train))],
bottom=bottom_padding,
width=bar_width,
label='CRIM')
# 添加坐标轴标签
ax.set_xlabel('CRIM Values')
ax.set_ylabel('Price Correlation Matrix')
# 设置横轴刻度单位
ax.yaxis.set_major_locator(multipleLocator(1))
# 配置图表标题和字体大小
ax.set_title('Relationship between CRIM and Property Prices',
fontdict={'fontsize': 18})
# 添加边框和内填充
ax.tick_params(axis="both", which="major", color=".5", alpha=0.5)
ax.fill_between(x=[0, len(feature_weights_train) - bar_width],
y1=[price_corr_matrix[0][0] for _ in range(len(feature_weights_train) - bar_width)],
y2=[price_corr_matrix[len(feature_weights_train) - bar_width][0] for _ in range(len(feature_weights_train) - bar_width)],
facecolor="skyblue",
edgecolor=".5",
linewidth=0.5)
# 最后添加背景颜色和阴影效果
ax.get_figure().set_facecolor("white")
ax.get_figure().set_edgecolor("black")
# 显示图表
plt.show()
请注意,这个示例代码使用了一个简单的柱状图,并未考虑潜在的异常情况(如缺失特征或特征值变化不一致)或数值特征的范围限制(如正负数)。如果您想要处理更复杂的情况,可能需要添加更多的数据预处理步骤,如归一化特征值、进行特征工程、进行异常检测等。此外,您也可以选择使用其他图形库(如 Matplotlib, Seaborn 等)或 API 来实现实时绘制图,这些工具提供了更多灵活的可视化功能和模型调优选项。