Python 将训练数组馈送到tensorflow时出错
我试图训练一个神经网络,但我不断地得到输入错误 在我的问题中,X被定义为286784268d数组。每个数据点都有一个int标签,我将其转换为category。我试图对14个类进行分类。详情如下:Python 将训练数组馈送到tensorflow时出错,python,tensorflow,keras,deep-learning,neural-network,Python,Tensorflow,Keras,Deep Learning,Neural Network,我试图训练一个神经网络,但我不断地得到输入错误 在我的问题中,X被定义为286784268d数组。每个数据点都有一个int标签,我将其转换为category。我试图对14个类进行分类。详情如下: X_train.shape = (286784,) X_train[0].shape = (268,) y_train.shape = (286784, 14) y_train[0].shape = (14,) X_test.shape = (71696,) X_test[0].shape = (26
X_train.shape = (286784,)
X_train[0].shape = (268,)
y_train.shape = (286784, 14)
y_train[0].shape = (14,)
X_test.shape = (71696,)
X_test[0].shape = (268,)
y_test.shape = (71696, 14)
y_test[0].shape = (14,)
要创建数据并训练我的模型,以下是我的代码:
from ast import literal_eval
df = pd.read_csv('data.csv')
# convert string representation to list
df['X'] = df.X.apply(lambda x: literal_eval(str(x)))
# convert each item (list) to numpy array
def convert_to_numpy(df):
return np.asarray(df.X)
df['X'] = df.apply(convert_to_numpy, axis=1)
# train test split
X_train, X_test, y_train, y_test = train_test_split(df.X.values, df.y.values, test_size = 0.2, random_state = 42)
# convert y to categorical
y_train, y_test = to_categorical(y_train, dtype='float32'), to_categorical(y_test, dtype='float32')
# build model
def compile_and_fit(X_train, y_train, X_val, y_val):
model = Sequential()
model.add(Dense(200, activation='relu'))
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(3, activation='softmax'))
opt = keras.optimizers.Adam(learning_rate=0.005)
model.compile(optimizer=opt,
loss='categorical_crossentropy',
metrics=['accuracy'])
model_history = model.fit(X_train,
y_train,
epochs=1,
batch_size=32,
validation_data=(X_val, y_val))
print(model.summary())
return model_history, model
# run model
hist, model = compile_and_fit(X_train,
y_train,
X_test,
y_test)
然后我得到这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-c6305c0e6184> in <module>()
3 y_train,
4 X_test,
----> 5 y_test)
6
7 # visualize training
14 frames
<ipython-input-15-c22b1f6df674> in compile_and_fit(X_train, y_train, X_val, y_val)
18 epochs=1,
19 batch_size=32,
---> 20 validation_data=(X_val, y_val))
21 print(model.summary())
22
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1061 use_multiprocessing=use_multiprocessing,
1062 model=self,
-> 1063 steps_per_execution=self._steps_per_execution)
1064
1065 # Container that configures and calls `tf.keras.Callback`s.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1115 use_multiprocessing=use_multiprocessing,
1116 distribution_strategy=ds_context.get_strategy(),
-> 1117 model=model)
1118
1119 strategy = ds_context.get_strategy()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
263 **kwargs):
264 super(TensorLikeDataAdapter, self).__init__(x, y, **kwargs)
--> 265 x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
266 sample_weight_modes = broadcast_sample_weight_modes(
267 sample_weights, sample_weight_modes)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _process_tensorlike(inputs)
1019 return x
1020
-> 1021 inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
1022 return nest.list_to_tuple(inputs)
1023
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
633
634 return pack_sequence_as(
--> 635 structure[0], [func(*x) for x in entries],
636 expand_composites=expand_composites)
637
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
633
634 return pack_sequence_as(
--> 635 structure[0], [func(*x) for x in entries],
636 expand_composites=expand_composites)
637
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _convert_numpy_and_scipy(x)
1014 if issubclass(x.dtype.type, np.floating):
1015 dtype = backend.floatx()
-> 1016 return ops.convert_to_tensor(x, dtype=dtype)
1017 elif scipy_sparse and scipy_sparse.issparse(x):
1018 return _scipy_sparse_to_sparse_tensor(x)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1497
1498 if ret is None:
-> 1499 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500
1501 if ret is NotImplemented:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_conversion_registry.py in _default_conversion_function(***failed resolving arguments***)
50 def _default_conversion_function(value, dtype, name, as_ref):
51 del as_ref # Unused.
---> 52 return constant_op.constant(value, dtype, name=name)
53
54
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
262 """
263 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 264 allow_broadcast=True)
265
266
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
273 with trace.Trace("tf.constant"):
274 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 275 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
276
277 g = ops.get_default_graph()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
299 """Implementation of eager constant."""
--> 300 t = convert_to_eager_tensor(value, ctx, dtype)
301 if shape is None:
302 return t
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
3年列车,
4 X_检验,
---->5(y_试验)
6.
7#形象化培训
14帧
在编译和拟合中(X_列,y_列,X_val,y_val)
18个时代=1,
19批次尺寸=32,
--->20验证数据=(X值,y值))
21打印(model.summary())
22
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in_method_包装(self,*args,**kwargs)
106定义方法包装(self,*args,**kwargs):
107如果不是self._处于_multi_worker_模式():#pylint:disable=受保护的访问
-->108返回方法(self、*args、**kwargs)
109
110#已经在“运行分配协调器”内部运行了。
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self、x、y、批大小、历元、冗余、回调、验证拆分、验证数据、无序、类权重、样本权重、初始历元、每历元的步长、验证步骤、验证批量大小、验证队列频率、最大大小、工作人员、使用多处理)
1061使用多处理=使用多处理,
1062型号=自我,
->1063步每执行=自。_步每执行)
1064
1065#配置和调用`tf.keras.Callback`的容器。
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data\u adapter.py in\uuuuuu init\uuuuuuuuu(self,x,y,sample\u weight,batch\u size,steps\u per\u epoch,initial\u epoch,epoch,shuffle,class\u weight,max\u queue\u size,worker,use\u多处理,model,steps\u per\u执行)
1115使用多处理=使用多处理,
1116 distribution_strategy=ds_context.get_strategy(),
->1117型号=型号)
1118
1119 strategy=ds_context.get_strategy()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data\u adapter.py in\uuuuuuuu init\uuuuuuuuuuuu(self、x、y、样本权重、样本权重、模式、批量大小、年代、步骤、洗牌、**kwargs)
263**夸尔格):
264超级(TensorLikeDataAdapter,self)。\uuuuu初始化(x,y,**kwargs)
-->265 x,y,样本权重=_过程类张量((x,y,样本权重))
266样本重量模式=广播重量模式(
267样本重量,样本重量模式)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data\u adapter.py in\u process\u tensorlike(输入)
1019返回x
1020
->1021 inputs=nest.map\u结构(\u convert\u numpy\u和\u scipy,inputs)
1022返回嵌套。列表到元组(输入)
1023
/映射结构中的usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py(func,*structure,**kwargs)
633
634返回包\u序列\u组件(
-->635结构[0],[func(*x)表示条目中的x],
636扩展_复合材料=扩展_复合材料)
637
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in(.0)
633
634返回包\u序列\u组件(
-->635结构[0],[func(*x)表示条目中的x],
636扩展_复合材料=扩展_复合材料)
637
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data\u adapter.py in\u convert\u numpy\u和\u scipy(x)
1014如果issubclass(x.dtype.type,np.floating):
1015 dtype=backend.floatx()
->1016返回运算。将\u转换为\u张量(x,dtype=dtype)
1017 elif scipy_sparse和scipy_sparse.issparse(x):
1018返回稀疏张量(x)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(值、数据类型、名称、as_ref、首选数据类型、数据类型提示、ctx、接受的结果类型)
1497
1498如果ret为无:
->1499 ret=conversion\u func(值,dtype=dtype,name=name,as\u ref=as\u ref)
1500
1501如果未实施ret:
/函数中的usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor\u conversion\u registry.py(***解析参数失败***)
50 def默认转换函数(值、数据类型、名称,作为参考):
51 del as_ref#未使用。
--->52返回常量\运算常量(值,数据类型,名称=名称)
53
54
/常量中的usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py(值、数据类型、形状、名称)
262 """
263返回\u常量\u impl(值、数据类型、形状、名称、验证\u形状=False,
-->264允许_广播=真)
265
266
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant\u op.py in\u constant\u impl(值、数据类型、形状、名称、验证形状、允许广播)
273带有trace.trace(“tf.constant”):
274返回_常量_eager _impl(ctx、值、数据类型、形状、验证形状)
-->275返回_常量_eager _impl(ctx、值、数据类型、形状、验证形状)
276
277 g=ops.get\u default\u graph()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant\u op.py in\u constant\u eager\u impl(ctx、值、数据类型、形状、验证形状)
298定义常量(ctx、值、数据类型、形状、验证形状):
299“急切常数的实现”
-->300 t=转换为张量(值、ctx、数据类型)
301如果形状为“无”:
302返回t
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value,ctx,dtype
import tensorflow as tf
mylist = [
[[1, 2, 3], [5, 4, 6]],
[[2, 8, 9]]
]
rt = tf.ragged.constant(mylist)
features_set = rt.to_tensor()
features_set
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[1, 2, 3],
[5, 4, 6]],
[[2, 8, 9],
[0, 0, 0]]], dtype=int32)>
%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
import numpy as np
from numpy import loadtxt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Concatenate
input1 = Input(shape=(3,))
# define model
x = Dense(12, input_shape = (2,), activation='relu')(input1)
x = Dense(8, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=input1, outputs=x)
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
features_set = [
[[1, 2, 3], [5, 4, 6]],
[[2, 8, 9]]
]
labels = [5, 8]
# Fit the model
model.fit(x=features_set, y=labels, epochs=150, batch_size=10, verbose=0)
2.3.0
Model: "functional_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 3)] 0
_________________________________________________________________
dense (Dense) (None, 12) 48
_________________________________________________________________
dense_1 (Dense) (None, 8) 104
_________________________________________________________________
dense_2 (Dense) (None, 1) 9
=================================================================
Total params: 161
Trainable params: 161
Non-trainable params: 0
_________________________________________________________________
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-ecade355bf68> in <module>()
35
36 # Fit the model
---> 37 model.fit(x=features_set, y=labels, epochs=150, batch_size=10, verbose=0)
14 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).
%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
import numpy as np
from numpy import loadtxt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Concatenate
input1 = Input(shape=(3,))
# define model
x = Dense(12, input_shape = (2,), activation='relu')(input1)
x = Dense(8, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=input1, outputs=x)
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
rt = tf.ragged.constant([
[[1, 2, 3], [5, 4, 6]],
[[2, 8, 9]]
])
features_set = rt.to_tensor()
labels = np.asarray([5, 8])
# Fit the model
model.fit(x=features_set, y=labels, epochs=150, batch_size=10, verbose=0)
2.3.0
Model: "functional_35"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_18 (InputLayer) [(None, 3)] 0
_________________________________________________________________
dense_51 (Dense) (None, 12) 48
_________________________________________________________________
dense_52 (Dense) (None, 8) 104
_________________________________________________________________
dense_53 (Dense) (None, 1) 9
=================================================================
Total params: 161
Trainable params: 161
Non-trainable params: 0
_________________________________________________________________
WARNING:tensorflow:Model was constructed with shape (None, 3) for input Tensor("input_18:0", shape=(None, 3), dtype=float32), but it was called on an input with incompatible shape (None, 2, 3).
WARNING:tensorflow:Model was constructed with shape (None, 3) for input Tensor("input_18:0", shape=(None, 3), dtype=float32), but it was called on an input with incompatible shape (None, 2, 3).
<tensorflow.python.keras.callbacks.History at 0x7f181ad9e400>