使用keras训练模型时发生TypeError和ValueError

时间:2019-06-17 03:34:09

标签: python tensorflow keras deep-learning conv-neural-network

我正在用keras训练卷积模型。而且我遇到了两个我从未遇到过的错误,而且我无法在线找到解决方案。这是整个错误回溯输出:

  

警告:在标志解析之前记录到stderr。       W0617 03:18:36.916876 139928036104064 deprecation_wrapper.py:119]来自   /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:74:   名称tf.get_default_graph已弃用。请用   改为使用tf.compat.v1.get_default_graph。

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

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:517:   不推荐使用名称tf.placeholder。请用   改为使用tf.compat.v1.placeholder。

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

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4185:   名称tf.truncated_normal已弃用。请用   改为tf.random.truncated_normal。

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

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:174:   名称tf.get_default_session已弃用。请用   改为使用tf.compat.v1.get_default_session。

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

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:181:   不推荐使用名称tf.ConfigProto。请用   而是使用tf.compat.v1.ConfigProto。

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

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:1834:   不推荐使用名称tf.nn.fused_batch_norm。请用   而不是tf.compat.v1.nn.fused_batch_norm。

/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]来自/usr/local/lib/python3.6/dist-packages/keras/optimizers.py:790:   不推荐使用名称tf.train.Optimizer。请用   改为使用tf.compat.v1.train.Optimizer。

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(来自   tensorflow.python.ops.array_ops)已弃用,并将在   未来版本。       更新说明:       在2.0中使用tf.where,其广播规则与np.where相同       _________________________________________________________________       图层(类型)输出形状参数#
      ================================================== ===============       input_1(InputLayer)(无,28,28,1)0
      _________________________________________________________________       conv2d_1(Conv2D)(无,28、28、36)180
      _________________________________________________________________       batch_normalization_1(批次(无,28、28、36)144
      _________________________________________________________________       activation_1(激活)(无,28、28、36)0
      _________________________________________________________________       conv2d_2(Conv2D)(无,28、28、18)2610
      _________________________________________________________________       batch_normalization_2(批次(无,28、28、18)72
      _________________________________________________________________       activation_2(激活)(无,28、28、18)0
      _________________________________________________________________       conv2d_3(Conv2D)(无,28、28、9)657
      _________________________________________________________________       batch_normalization_3(批次(无,28、28、9)36
      _________________________________________________________________       activation_3(激活)(无,28、28、9)0
      _________________________________________________________________       flatten_1(Flatten)(无,7056)0
      _________________________________________________________________       density_1(Dense)(None,345)2434665
      _________________________________________________________________       activation_4(激活)(无,345)0
      ================================================== ===============       参数总计:2,438,364       可训练的参数:2,438,238       不可训练参数:126       _________________________________________________________________       训练3450000个样本,验证345000个样本       时代1/100       -------------------------------------------------- -------------------------       TypeError跟踪(最近一次通话)        getitem 中的/usr/local/lib/python3.6/dist-packages/tables/array.py(自已,密钥)           660#首先,尝试常规选择       -> 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)。我以前训练过类似的模型,但从未遇到过这样的错误,例如无效的索引切片和操作数不能一起广播。我正在使用Google Colab进行培训。培训代码为:

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))

1 个答案:

答案 0 :(得分:0)

将其添加到代码开头指定TensorFlow版本,因为colab中的默认版本现在是Tensorflow V2.0

%tensorflow_version 1.x

将tensorflow导入为tf

print(tf。版本