为什么这个张量流模型成本最大化而不是最小化?

时间:2016-12-17 18:08:31

标签: python neural-network tensorflow deep-learning

我是初学者,在tensorflow中,我想实现模型来识别新灰度图像的任何像素的rgb,所以我用灰色图像像素训练它的彩色像素,但成本最大化而不是最小化,所以我的错误是什么逻辑还是代码?

n_nodes_hl1 = 50
n_outputs = 3
x = tf.placeholder('float',[1,1])#pixel in gray
y = tf.placeholder('float',[1,3])# rgb chanels

def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([1, n_nodes_hl1])),
                  'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}


output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_outputs])),
                'biases':tf.Variable(tf.random_normal([n_outputs])),}

l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)

output = tf.matmul(l1,output_layer['weights']) + output_layer['biases']

return output
#=======================================
def train_neural_network():
prediction = neural_network_model(x)
cost = tf.nn.softmax_cross_entropy_with_logits(prediction,y)
optimizer = tf.train.AdamOptimizer().minimize(cost)

nm_epochs = 500
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(nm_epochs):
        epoch_loss = 0
        for i in range(len(images_grey)):
            for xx in range(images_grey[i].width):
                for yy in range(images_grey[i].height):                       
                     _, c = sess.run([optimizer, cost], feed_dict={x: np.reshape(images_grey[i].getpixel((xx,yy)),[1,1]), y:np.reshape(images_colored[i].getpixel((xx,yy)),[1,3])})
                     epoch_loss += c
        print('Epoch', epoch, 'completed out of',nm_epochs,'loss:',epoch_loss)

train_neural_network()

和images_colored = [],images_grey = []填充了另一个函数的图像。

1 个答案:

答案 0 :(得分:0)

我了解您所面临的学习曲线,并感谢您分享此问题的模型。您可以尝试在训练循环中的每几个时期绘制预测图像:

n_iterations = 500
batch_size = 50
with tf.Session() as sess:
# Here we tell tensorflow that we want to initialize all
# the variables in the graph so we can use them
# This will set W and b to their initial random normal value.
sess.run(tf.initialize_all_variables())

# We now run a loop over epochs
prev_training_cost = 0.0
for it_i in range(n_iterations):
    idxs = np.random.permutation(range(len(xs)))
    n_batches = len(idxs) // batch_size
    for batch_i in range(n_batches):
        idxs_i = idxs[batch_i * batch_size: (batch_i + 1) * batch_size]
        sess.run(optimizer, feed_dict={X: xs[idxs_i], Y: ys[idxs_i]})

    training_cost = sess.run(cost, feed_dict={X: xs, Y: ys})
    print(it_i, training_cost)

    if (it_i + 1) % 20 == 0:
        ys_pred = Y_pred.eval(feed_dict={X: xs}, session=sess)
        fig, ax = plt.subplots(1, 1)
        img = np.clip(ys_pred.reshape(img.shape), 0, 255).astype(np.uint8)
        plt.imshow(img)
        plt.show()

我已经通过Parag K Mital下的免费在线课程,我认为您可能会觉得在您的旅程中很有用。有一个特定的讲座,你训练网络绘制RBG图像(讲座2)。您可以改变网络规模和学习速率,并逐步了解网络的运行情况:

https://github.com/pkmital/CADL/blob/master/session-2/lecture-2.ipynb

取决于图像域(例如,面部),在代表性数据集上训练的深度网络可以提供优越的性能并且能够基于灰度级强度值更好地预测每个像素的RBG值。视频也可能有用,因为Parag在打破概念方面做得非常好。

让我知道这个资源是否有用并且好运!