我想分类 如果输入数据小于200,则输出为(0,1) 如果输入数据大于200,则输出为(1,0)
输入值为顺序整数值,层为5。你知道吗
隐藏层使用sigmoid,最后一个隐藏层使用softmax函数
损失函数是降维均值和梯度下降训练
import numpy as np
import tensorflow as tf
def set_x_data():
x_data = np.array([[50]
, [60]
, [70]
, [80]
, [90]
, [110]
, [120]
, [130]
, [140]
, [150]
, [160]
, [170]
, [180]
, [190]
, [200]
, [210]
, [220]
, [230]
, [240]
, [250]
, [260]
, [270]
, [280]
, [290]
, [300]
, [310]
, [320]
, [330]
, [340]
, [350]
, [360]
, [370]
, [380]
, [390]])
return x_data
def set_y_data(x):
y_data = np.array([[0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [0, 1]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]
, [1, 0]])
return y_data
def set_bias(efficiency):
arr = np.array([efficiency])
return arr
W1 = tf.Variable(tf.random_normal([1, 5]), name='weight1')
W2 = tf.Variable(tf.random_normal([5, 5]), name='weight2')
W3 = tf.Variable(tf.random_normal([5, 5]), name='weight3')
W4 = tf.Variable(tf.random_normal([5, 5]), name='weight4')
W5 = tf.Variable(tf.random_normal([5, 2]), name='weight5')
def inference(input, b):
hidden_layer1 = tf.sigmoid(tf.matmul(input, W1) + b)
hidden_layer2 = tf.sigmoid(tf.matmul(hidden_layer1, W2) + b)
hidden_layer3 = tf.sigmoid(tf.matmul(hidden_layer2, W3) + b)
hidden_layer4 = tf.sigmoid(tf.matmul(hidden_layer3, W4) + b)
out_layer = tf.nn.softmax(tf.matmul(hidden_layer4, W5) + b)
return out_layer
def loss(hypothesis, y):
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), reduction_indices=[1]))
return cross_entropy
def train(loss):
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train = optimizer.minimize(loss)
return train
x_data = set_x_data(1)
y_data = set_y_data(0)
b_data = set_bias(0.8)
x= tf.placeholder(tf.float32, shape=[None, 1])
y= tf.placeholder(tf.float32, shape=[None, 2])
b = tf.placeholder(tf.float32, shape=[None])
hypothesis = inference(x, b)
loss = loss(hypothesis, y)
train = train(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(W1))
for step in range(2000):
sess.run(train, feed_dict={x:x_data, y:y_data, b:b_data})
print(sess.run(W1))
print(sess.run(hypothesis, feed_dict={x:np.array([[1000]]), b:b_data}))
当我在训练前和训练后打印W1时,值并没有特别改变,当输入=1000时测试,该值并没有达到我的预期。我认为值接近(1,0),但结果接近(0.5,0.5)
我猜错误来自损失函数,因为它是从这里和那里复制的,但我不能确定
上面的代码只是简化了我的代码,但我想我必须显示我真正的代码
代码太长,所以我创建了新的帖子
classifying data by tensorflow but accuracy value didn't change
上述网络的训练中存在一些问题,但是只要做一些更改,就可以实现一个获得this decision function的网络
(The plot in the link显示2级的分数,即如果x>;200)
此网络中有待改进的问题列表:
培训数据非常缺乏(只有34分!)这通常太小,尤其是对于您的案例中的5层网络。在网络中,通常需要比参数多得多的输入样本。尝试添加更多的输入值并减少层的数量(如下面的代码所示-我使用了浮点而不是整数来获得更多的点,但我认为它仍然是兼容的)。
输入范围通常需要缩放(下面我尝试了一个除以常数的超简单缩放)。这是因为您通常希望避免高范围的变量(特别是当您通过具有软最大非线性的许多层时,这将破坏包含在非常高或非常低的值中的信息)。在更高级的情况下,您可能需要执行最小-最大缩放或z-分数。
尝试更多的时代(并尝试绘制损失函数值的演变)。在给定的历元数下,损失函数的优化没有收敛。下面我做了10倍多的纪元。看看下面的代码现在是如何在this plot中聚合的(看看2000个时代是如何不够的):
有帮助的是对(x,y)数据进行洗牌。虽然这在这种情况下并不重要,但它收敛得更快(见Le Cun的论文“Efficient Backprop”)。在更严重的例子中,它通常是需要的。
重要的是,我想你希望
b
是一个参数,而不是一个常数,不是吗?网络的偏差通常也与乘性权重一起优化。(另外,对所有隐藏层使用单一的共享偏移也是不常见的。)下面是代码。注意,可能会有进一步的改进,但这些技巧最终会得到所需的决策函数。你知道吗
我添加了一些内联注释,以指示相对于原始的更改。我希望你觉得这些建议很有见地!你知道吗
代码:
显示上述决策函数的代码:
相关问题 更多 >
编程相关推荐