Tensorflow 张量流预测是恒定的

Tensorflow 张量流预测是恒定的,tensorflow,predict,Tensorflow,Predict,大家好,我用CNN预测128个值的向量有问题。 输入为48x48图像。这些预测似乎都是一样的 import tensorflow as tf from tools import load_datas sess = tf.InteractiveSession() TRAIN_SIZE = 100000 VAL_SIZE = 10000 TEST_SIZE = 10000 IMAGE_SIZE = 48 * 48 LABEL_SIZE = 128 # placeholder for image

大家好,我用CNN预测128个值的向量有问题。 输入为48x48图像。这些预测似乎都是一样的

import tensorflow as tf
from tools import load_datas

sess = tf.InteractiveSession()

TRAIN_SIZE = 100000
VAL_SIZE = 10000
TEST_SIZE = 10000

IMAGE_SIZE = 48 * 48
LABEL_SIZE = 128
# placeholder for images, labels and dropout
train_x, train_y, val_x, val_y, test_x = load_datas(
TRAIN_SIZE, VAL_SIZE, TEST_SIZE, IMAGE_SIZE, LABEL_SIZE)

train_x = train_x[::10]
train_y = train_y[::10]

def weight_variable(shape):
    initial = tf.truncated_normal(shape)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.random_normal(shape)# tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                    strides=[1, 2, 2, 1], padding='SAME')

x = tf.placeholder(tf.float32, shape=[None, 48 * 48])
y_ = tf.placeholder(tf.float32, shape=[None, 128])
global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')

W_conv1 = weight_variable([5, 5, 1, 1])
b_conv1 = bias_variable([1])
x_image = tf.reshape(x, [-1,48,48,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

input_n = 24 * 24 * 1
h_pool1_reshaped = tf.reshape(h_pool1, [-1, input_n])

keep_prob = tf.placeholder(tf.float32)

W_fc2 = weight_variable([input_n, 128])
b_fc2 = bias_variable([128])

y_conv = tf.matmul(h_pool1_reshaped, W_fc2) + b_fc2

loss = tf.reduce_sum(tf.square(y_conv - y_))
train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(loss)
accuracy = tf.reduce_mean(tf.square(y_conv - y_))
sess.run(tf.global_variables_initializer())

batch_size = 100
n_batches = int(train_x.shape[0] / batch_size)
current_position = 0
epochs = 10
for i in range(n_batches * epochs):
    if current_position >= train_x.shape[0]:
        current_position = 0
    x_batch = train_x[current_position: current_position + batch_size, :]
    y_batch = train_y[current_position: current_position + batch_size, :]
    train_step.run(feed_dict={x: x_batch, y_: y_batch, keep_prob: 0.5})
    if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict={x:x_batch, y_: y_batch, keep_prob: 1.0})
        print("step %d, accuracy %g"%(i, train_accuracy))
    current_position += batch_size

preds = sess.run(y_conv, feed_dict={x: val_x})
print(preds)
为了简单起见,只使用了一个简单的conva层,但如果cnn更复杂,则使用相同的层。 有人遇到过同样的问题吗?(尝试了更多的时代和更小的学习率,但仍然是相同的问题)

谢谢

编辑: 我修改了代码,使其更易于分析
看起来预测值等于最后的偏差…

会话的缩进是否正确?当您迭代批处理时,它看起来没有关联,因此我认为
tf.session()
中的会话实际上仍然处于活动状态。这也适用于运行预测时。发布代码时出错,但所有内容都缩进良好