ValueError:无法将NumPy数组转换为张量(不受支持的对象类型int)。 Tensorflow 1.15运行。 Tensorflow 2.x中断

时间:2020-07-26 22:20:08

标签: python numpy tensorflow migration

系统:Linux Manjaro

完整的错误是:

    history = clf.fit(
  File "/usr/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 795, in fit
    data_adapter.train_validation_split((x, y, sample_weight),
  File "/usr/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1337, in train_validation_split
    train_arrays = nest.map_structure(
  File "/usr/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 617, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/usr/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 617, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/usr/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1334, in _split
    t = ops.convert_to_tensor_v2(t)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1278, in convert_to_tensor_v2
    return convert_to_tensor(
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 261, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

我正在使用以下代码训练CNN:

X2 = np.append(self.data.Img_Lr2_Train, self.data.Img_Lr2_Val, axis=0)
Y  = np.append(self.data.Y_Train, self.data.Y_Val, axis=0)
Y  = np.multiply(Y, 1)
X2, Y = shuffle(X2, Y, random_state=self.seed)
X2 = X2.reshape(X2.shape[0], 11, 7, 1)

history = clf.fit(
                  x=X2,
                  y=Y,
                  epochs=self.nEp,
                  batch_size=self.bt,
                  # shuffle=True,
                  validation_split=0.33,
                  callbacks=callbacks_list,
                  class_weight=class_weights
                  )

根据(Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float))的讨论,我检查了变量的类型和形状: 都是numpy数组,符合预期。

主要问题是,当我使用tensorflow 1.15运行它时,我没有任何问题。 但是,如果我尝试使用tensorflow 2.2运行,则会收到错误消息。 我尝试使用以下命令从tf_v1-> tf_v2迁移代码 tf_upgrade_v2 DEFAULTSAFETY模式下, 在两种情况下,该过程均成功结束。 但这并不能解决问题。

您知道问题可能在哪里吗?

提前谢谢

1 个答案:

答案 0 :(得分:0)

我不使用validation_split解决了这个问题, 相反,我要做的是:

X2_train= self.data.Img_Lr2_Train,
X2_val =  self.data.Img_Lr2_Val
Y_train=self.data.Y_Train, 
Y_val = self.data.Y_Val

X2_train = np.reshape(X2_train.shape[0], 11, 7, 1)
Y_train = np.reshape(Y_train.shape[0], 1)

# same with val arrays

X2_train =np.asarray(X2_train)
Y_train = np.asarray(Y_train)

我知道最后一步看起来很奇怪,因为所有数组都已经是np.arrays了, 但是成功了 最后,我将fit函数更改为:

history = clf.fit(
                  x=X2_train,
                  y=Y_train,
                  validation_data=(X2_val,Y_val),
                  epochs=self.nEp,
                  batch_size=self.bt,
                  # shuffle=True,
                  callbacks=callbacks_list,
                  class_weight=class_weights
                  )