1
我实现了一个基本的MLP,我希望它来预测用户生成的数据集,但预测看起来如下:TensorFlow神经网络输出的线性函数
。
我不知道为什么......我在隐层的非线性,我试图多次启用(ReLU
,tanh
,sigmoid
),尝试了不同的优化器,不同的学习速度,各种体系结构(多层,层次少,辍学),但我从来没有得到这个权利。
请注意,我确实相信这可能是因为我如何计算最后的预测(pred = sess.run(out, feed_dict={inputs:X.reshape(n_input, 1)})
),因为它可能不正确,但我不知道为什么。我还尝试了其他方法,例如用w = sess.run(weights)
提取重量,然后将它们与输入一起输入model()
函数,但没有任何结果。
另外,在监视错误时,错误在各个时期之间降低。
任何想法?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# Architecture
input_size = 1
output_size = 1
h1_size = 20
h2_size = 50
# 2 hidden layers network
def model(inputs, weights):
out1 = tf.nn.relu(tf.matmul(inputs, weights['h1']))
out2 = tf.nn.relu(tf.matmul(out1, weights['h2']))
return tf.matmul(out2, weights['h3'])
# Inputs/label placeholders
inputs = tf.placeholder('float', shape=(None, input_size))
labels = tf.placeholder('float', shape=(None, output_size))
# Learnable weights
weights = {
'h1': tf.Variable(tf.random_normal(shape=(input_size, h1_size))),
'h2': tf.Variable(tf.random_normal(shape=(h1_size, h2_size))),
'h3': tf.Variable(tf.random_normal(shape=(h2_size, output_size))),
}
# Stores the result from the net
out = model(inputs, weights)
# Cost and optimisation
cost = tf.reduce_mean(tf.square(out - labels))
opt = tf.train.AdadeltaOptimizer()
opt_operation = opt.minimize(cost)
# Generate some data
n_input = 1000
X = np.linspace(0, 1, n_input).astype('f')
y = X + 5 * np.sin(X * 10)
y /= max(y)
# Train
epochs = 2000
lr = 0.0000001
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(epochs):
_, c = sess.run([opt_operation, cost], feed_dict={
inputs: X.reshape(n_input, 1),
labels: y.reshape(n_input, 1),
})
if not epoch % int(epochs/20):
print(c)
pred = sess.run(out, feed_dict={inputs:X.reshape(n_input, 1)})
plt.scatter(X, pred, color='red', label='prediction')
plt.scatter(X, y, label='data')
plt.legend()
plt.show()