我正在尝试学习 Keras 并创建了一个简单的网络。特征数据为 [1, 2, 3, 4, 5],标签为 [7, 9, 11, 13, 15] - 或斜率为 2,截距为 5 的直线(Y = X * 2 + 5)。
这是 Keras 网络:
# simple keras example
#
# This solves for a line
import numpy as np
import keras
# configuration variables
samples = 5
base = 1
slope = 2
intercept = 5
# hyper-parameters
learning_rate = 0.01
epochs = 2000
model = keras.Sequential()
model.add(keras.layers.Dense(1, input_dim=1, activation=keras.activations.linear))
sgd = keras.optimizers.SGD(learning_rate=learning_rate)
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['mean_absolute_error'])
X = np.array(range(base, base+samples))
Y = X * slope + intercept
model.fit(X, Y, epochs=epochs, batch_size=samples)
loss, accuracy = model.evaluate(X, Y)
print('Loss: ', loss, ' Accuracy: ', accuracy)
k_slope = model.layers[0].get_weights()[0]
k_intercept = model.layers[0].get_weights()[1]
print('slope: ', k_slope, ' intercept: ', k_intercept)
斜率在第一个时期结束为 -0.1879,并且没有进展。我怀疑我缺少参数或设置,或者可能是模型上的函数调用。但我无法弄清楚它是什么。
这是我试图在 Keras 中重现的张量流网络。该网络在大约 1300 个时期收敛到正确答案:
#simple linear regression with tensorflow
#
# This solves for a line
#
import tensorflow as tf
import numpy as np
# configuration variables
samples = 5
base = 1
slope = 2
intercept = 5
# hyper-parameters
learning_rate = 0.01
epochs = 2000
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
m = tf.Variable(0.0)
b = tf.Variable(0.0)
pred = tf.add(tf.multiply(x, m), b)
cost = tf.reduce_mean(tf.abs(y - pred))
me_first = tf.global_variables_initializer()
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
session = tf.Session()
session.run(me_first)
for i in range(epochs):
X = np.array(range(base, base+samples))
Y = X * slope + intercept
t_slope, t_intercept, total_err, opt = session.run([m, b, cost, optimizer], feed_dict={x:X, y:Y})
print('iter: ', i, ' intercept: ', t_intercept, ' slope: ', t_slope, ' error: ', total_err)