在本机Tensorflow中使用BatchNormalization训练Keras模型时面临的问题

时间:2018-06-26 05:32:13

标签: python tensorflow keras

我使用Keras编写了一个包含BatchNormalization层的神经网络。

当我用model.fit进行训练时,一切都很好。如此处所述,在天真Tensorflow中进行训练时,训练过程十分艰辛!

我意识到我需要explicitly run the BatchNormalization operations。当我尝试实施上一个链接中提供的解决方案时,似乎遇到了一些问题:

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

from tensorflow.python.keras import optimizers
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D
from tensorflow.python.keras.layers import BatchNormalization
from tensorflow.python.keras.regularizers import l2

sess = tf.Session()
def train(sess):
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    print(update_ops)
    with tf.control_dependencies(update_ops):
        train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
    sess.run(tf.global_variables_initializer())
    history = []
    iterep = 500
    for i in range(iterep * 30):
        x_train, y_train = mnist.train.next_batch(100)
        x_train = x_train.reshape((100, 28, 28, 1))
        sess.run(train_step,feed_dict={'x:0': x_train,'y:0': y_train})
        if (i + 1) %  iterep == 0:
            epoch = (i + 1)/iterep
            tr = sess.run([loss, accuracy],
                          feed_dict={'x:0': mnist.train.images.reshape((55000, 28, 28, 1)),'y:0': mnist.train.labels})
            t = sess.run([loss, accuracy],
                         feed_dict={'x:0': mnist.test.images.reshape((10000, 28, 28, 1)),'y:0': mnist.test.labels})
            history += [[epoch] + tr + t]
            print(history[-1])
    return history

x = tf.placeholder('float32', (None, 28, 28, 1), name='x')
y = tf.placeholder('float32', (None, 10), name='y')
phase = tf.placeholder(tf.bool, name='phase')
weight_decay  = 0.0001
model = Sequential()
model.add(Conv2D(6, (5, 5), padding='valid', kernel_regularizer=l2(weight_decay), activation = 'relu', kernel_initializer='he_normal', input_shape=(28, 28, 1)))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Conv2D(16, (5, 5), padding='valid', activation = 'relu', kernel_initializer='he_normal'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(120, activation = 'relu', kernel_initializer='he_normal'))
model.add(Dense(84, activation = 'relu', kernel_initializer='he_normal'))
model.add(Dense(10, activation = 'softmax', kernel_initializer='he_normal'))
sgd = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
out = model(x)
with tf.name_scope('accuracy'):accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(out, 1)), 'float32'))
loss = tf.reduce_mean(tf.keras.backend.categorical_crossentropy(out, y))
print('Training ...')
history_lenet = train(sess)

试图执行以上代码,我遇到以下错误,并将所需的参数传递给张量操作,但似乎仍然抱怨我没有。

Traceback (most recent call last):
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1361, in _do_call
    return fn(*args)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1340, in _run_fn
    target_list, status, run_metadata)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 516, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float and shape [?,28,28,1]
         [[Node: conv2d_1_input = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ex.py", line 56, in <module>
    history_lenet = train(sess)
  File "ex.py", line 25, in train
    sess.run(train_step,feed_dict={'x:0': x_train,'y:0': y_train})
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 905, in run
    run_metadata_ptr)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1137, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1355, in _do_run
    options, run_metadata)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1374, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float and shape [?,28,28,1]
         [[Node: conv2d_1_input = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'conv2d_1_input', defined at:
  File "ex.py", line 41, in <module>
    model.add(Conv2D(6, (5, 5), padding='valid', kernel_regularizer=l2(weight_decay), activation = 'relu', kernel_initializer='he_normal', input_shape=(28, 28, 1)))
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 489, in add
    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 634, in Input
    input_tensor=tensor)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 543, in __init__
    name=name)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/layers/network.py", line 98, in __init__
    name=self.name)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1746, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3051, in _placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
    op_def=op_def)
  File "/Users/deepak/.pyenv/versions/3.6.4/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1650, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float and shape [?,28,28,1]
         [[Node: conv2d_1_input = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

0 个答案:

没有答案