Tensorflow 不学习 - 各种输入的相同答案

数据挖掘 Python 神经网络 深度学习 张量流 损失函数
2022-02-13 04:27:25

编码:

import numpy as np
import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split

x_data = np.linspace(0, 1000, 100000)
y_true = np.square(x_data)
y_true += np.random.randn(len(x_data))


feature_columns = [tf.feature_column.numeric_column('x', shape=[1])]
estimator = tf.estimator.DNNRegressor(feature_columns=feature_columns,         hidden_units=[2, 2], optimizer=lambda:
                                  tf.train.AdamOptimizer(
                                      learning_rate=0.001
                                  ))


X_train, X_test, y_train, y_test = train_test_split(x_data, y_true, test_size=0.3)

input_function = tf.estimator.inputs.numpy_input_fn({'x': X_train},y_train,
                                                batch_size=20, num_epochs=10000,
                                                shuffle=True)

train_input_function = tf.estimator.inputs.numpy_input_fn({'x': X_train},y_train,
                                                      batch_size=8, num_epochs=10000,
                                                      shuffle=False)
test_input_function = tf.estimator.inputs.numpy_input_fn({'x': X_test},y_test,
                                                     batch_size=8, num_epochs=10000,
                                                     shuffle=False)


estimator.train(input_fn=input_function, steps=1000)

train_metrics = estimator.evaluate(input_fn=train_input_function, steps=1000)
test_metrics = estimator.evaluate(input_fn=test_input_function, steps=1000)


print('TRAINING DATA METRICS')
print(train_metrics)
print()

print('TEST DATA METRICS')
print(test_metrics)
print()
###
new_data = np.linspace(0, 1000, 10)
input_function_predict = tf.estimator.inputs.numpy_input_fn({'x':new_data},     shuffle=False)
print(list(estimator.predict(input_fn=input_function_predict)))

给出以下输出:

TRAINING DATA METRICS
{'average_loss': 200498430000.0, 'label/mean': 332774.78, 'loss': 1603987400000.0, 'prediction/mean': 0.97833574, 'global_step': 1000}

TEST DATA METRICS
{'average_loss': 197508330000.0, 'label/mean': 332257.22, 'loss': 1580066700000.0, 'prediction/mean': 0.97833574, 'global_step': 1000}

[{'predictions': array([0.9783435], dtype=float32)}, {'predictions': array([0.9783435], dtype=float32)}, {'predictions': array([0.9783435], dtype=float32)}, {'predictions': array([0.9783435], dtype=float32)}, 
{'predictions': array([0.9783435],....

总而言之,损失是巨大的,因为 TF 预测所有 X 的 Y 值相同。代码有什么问题?

1个回答

一种可能性是DNNRegressor是 TensorFlow Estimator。TensorFlow Estimator 已被弃用,因为它们“可能会出现意外行为”。明确定义模型可能会更好。