我正在尝试按照this指南在Tensorflow张量中实现Keras图层。
我按如下方式创建占位符和模型。
sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(1)
# Initialize placeholders
x = tf.placeholder(tf.float32, shape=(BATCH_SIZE, 160, 320, 6))
y = tf.placeholder(tf.float32, [None])
# Create Model
x = Conv2D(64, kernel_size=3, strides=2, activation='relu', padding='same')(x)
x = Conv2D(64, kernel_size=3, strides=2, activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(x)
x = Conv2D(128, kernel_size=3, strides=2, activation='relu', padding='same')(x)
x = Conv2D(128, kernel_size=3, strides=2, activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(x)
x = Conv2D(256, kernel_size=3, strides=2, activation='relu', padding='same')(x)
x = Conv2D(256, kernel_size=3, strides=2, activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(x)
x = Flatten()(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.25)(x)
preds = Dense(1)(x)
我按如下方式运行它。
with sess.as_default():
for i in range(EPOCHS):
for batch in range(len(x_train) // BATCH_SIZE):
batch_x = x_train[batch * BATCH_SIZE:min((batch + 1) * BATCH_SIZE, len(x_train))]
batch_y = y_train[batch * BATCH_SIZE:min((batch + 1) * BATCH_SIZE, len(y_train))]
opt = sess.run(train_step, feed_dict={x: batch_x, y: batch_y})
我遇到错误“无法为Tensor'dropout_3 / dropout / mul:0'提供形状值(8,160,320,6),其形状为'(?,256)'”
答案 0 :(得分:0)
当您使用feed_dict时,它不会自动知道您要将其提供给您班级中出现的FIRST x,它可能会尝试将其提供给x出现的任何地方。特别是,看起来它正在将它输入到x:x = Dropout(0.25)(x)的最后一个赋值中。既然有大小? x 256,它不起作用。要解决此问题,请使用以下命令:
input = tf.placeholder(tf.float32, shape=(BATCH_SIZE, 160, 320, 6))
x = input
然后在你的feed_dict中,执行
feed_dict={input: batch_x, y: batch_y}