使用keras训练模型时出现类型错误和值错误

2024-10-01 13:45:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用keras训练一个卷积模型。我遇到了两个以前从未遇到过的错误,我无法在网上找到解决方法。以下是整个错误回溯输出:

WARNING: Logging before flag parsing goes to stderr. W0617 03:18:36.916876 139928036104064 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0617 03:18:36.956070 139928036104064 deprecation_wrapper.py:119] From

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0617 03:18:36.965062 139928036104064 deprecation_wrapper.py:119] From

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4185: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead.

W0617 03:18:37.005445 139928036104064 deprecation_wrapper.py:119] From

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

W0617 03:18:37.006534 139928036104064 deprecation_wrapper.py:119] From

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

W0617 03:18:39.945574 139928036104064 deprecation_wrapper.py:119] From

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:1834: The name tf.nn.fused_batch_norm is deprecated. Please use tf.compat.v1.nn.fused_batch_norm instead.

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:19: UserWarning: Update your `Model` call to the Keras 2 API:

Model(inputs=Tensor("in..., outputs=Tensor("ac...) W0617 03:18:40.237022 139928036104064 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

W0617 03:18:40.366632 139928036104064 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1250:

add_dispatch_support..wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.where in 2.0, which has the same broadcast rule as np.where _________________________________________________________________ Layer (type) Output Shape Param #
================================================================= input_1 (InputLayer) (None, 28, 28, 1) 0
_________________________________________________________________ conv2d_1 (Conv2D) (None, 28, 28, 36) 180
_________________________________________________________________ batch_normalization_1 (Batch (None, 28, 28, 36) 144
_________________________________________________________________ activation_1 (Activation) (None, 28, 28, 36) 0
_________________________________________________________________ conv2d_2 (Conv2D) (None, 28, 28, 18) 2610
_________________________________________________________________ batch_normalization_2 (Batch (None, 28, 28, 18) 72
_________________________________________________________________ activation_2 (Activation) (None, 28, 28, 18) 0
_________________________________________________________________ conv2d_3 (Conv2D) (None, 28, 28, 9) 657
_________________________________________________________________ batch_normalization_3 (Batch (None, 28, 28, 9) 36
_________________________________________________________________ activation_3 (Activation) (None, 28, 28, 9) 0
_________________________________________________________________ flatten_1 (Flatten) (None, 7056) 0
_________________________________________________________________ dense_1 (Dense) (None, 345) 2434665
_________________________________________________________________ activation_4 (Activation) (None, 345) 0
================================================================= Total params: 2,438,364 Trainable params: 2,438,238 Non-trainable params: 126 _________________________________________________________________ Train on 3450000 samples, validate on 345000 samples Epoch 1/100 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/tables/array.py in getitem(self, key) 660 # First, try with a regular selection --> 661 startl, stopl, stepl, shape = self._interpret_indexing(key) 662 arr = self._read_slice(startl, stopl, stepl, shape)

7 frames
TypeError: Non-valid index or slice: [1484419, 2231123, 3092786, 1496830, 493122, 736949, 1199629, 328357, 931000, 2946100, 415877,

1421951, 3421223, 2238167, 2940723, 1437219, 1839514, 2746665, 3359532, 3268348]

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tables/leaf.py in _point_selection(self, key)
    579             # handle negative indices
    580             idx = coords < 0
--> 581             coords[idx] = (coords + self.shape)[idx]
    582 
    583             # bounds check

ValueError: operands could not be broadcast together with shapes (20,) (4,)

训练数据的形状是(3450000,28,28,1),训练标签是(3450000,345)。我以前训练过类似的模型,但从未遇到过这样的错误,比如无效的索引片和操作数不能一起广播。我正在使用googlecolab进行培训。培训守则是:

def create_model():
  inp = Input((28,28,1))
  conv1 = Conv2D(36,(2,2),padding="same",kernel_initializer="glorot_normal")(inp)
  batch1 = BatchNormalization()(conv1)
  act1 = Activation("relu")(batch1)

  conv2 = Conv2D(18,(2,2),padding="same",kernel_initializer="glorot_normal")(act1)
  batch2 = BatchNormalization()(conv2)
  act2 = Activation("relu")(batch2)

  conv3 = Conv2D(9,(2,2),padding="same",kernel_initializer="glorot_normal")(act2)
  batch3 = BatchNormalization()(conv3)
  act3 = Activation("relu")(batch3)

  out = Flatten()(act3)
  out = Dense(345)(out)
  out = Activation("softmax")(out)

  model = Model(input = inp, output = out)
  adm = optimizers.Adagrad(lr=0.02)
  model.compile(optimizer=adm,loss="categorical_crossentropy",metrics=['accuracy'])
  model.summary()

  return model

if __name__ == "__main__":
  model = create_model()
  history = model.fit(x = train_X, y = train_Y, batch_size = 20, epochs = 100, validation_data = (eval_X, eval_Y))

Tags: frompynonebackendlibpackagesusrlocal