繁体   English   中英

无法将 numpy 数组转换为张量

[英]Failed to convert numpy array to tensor

我尝试在 python 中使用 keras/tensorflow 使用 model.fit 并收到此错误。 所有软件包安装正常,但收到转换为 numpy 阵列的问题

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
        259     ValueError: if called on a symbolic tensor.
        260   """
    --> 261   return _constant_impl(value, dtype, shape, name, verify_shape=False,
        262                         allow_broadcast=True)
        263 
    
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
        268   ctx = context.context()
        269   if ctx.executing_eagerly():
    --> 270     t = convert_to_eager_tensor(value, ctx, dtype)
        271     if shape is None:
        272       return t
    
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
         94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
         95   ctx.ensure_initialized()
    ---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
         97 
         98 
    
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

我的代码:

X = trainingpreds.values
y = traininglabels.values
# define the keras model model = Sequential()
model.add(Dense(20, input_dim=24, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y)

假设xy应该是int类型并且您在model.fit遇到错误,您可以使用转换为dtype

   x = tf.cast(trainingpreds.values, tf.int32)
   y = tf.cast(traininglabels, tf.int32)

要了解更多信息,请参阅

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM