大家好我有问题使用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)
为简单起见,只使用了一个简单的convo图层,但如果cnn更复杂,则使用相同的控件。 有人遇到过同样的问题吗? (尝试了更多的时代和更小的学习率,但仍然是同样的问题)
谢谢!
编辑: 我修改了代码,因此分析起来更简单 看起来预测等于最后的偏见...