keras 在 model.fit()错误中,它显示ValueError(< function>仅支持Tensor或NumPy数组)

xmq68pz9  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(314)

我是ML的新手,最后一行代码有一个错误:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense , Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
x = pickle.load(open("x.pickle","rb"))
y = pickle.load(open("y.pickle","rb"))
x=x/255.0
model = Sequential()

model.add(   Conv2D(64, (3,3), input_shape = x.shape[1:])   )
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2))) 

model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2))) 
model.add(Flatten())
model.add(Dense(64))

model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss="categorical_crossentropy",
              optimizer="adam",
              metrics=['accuracy'])
model.fit(x, y,batch_size=10,validation_split=0.1)

这就是错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_10844\1395261416.py in <module>
----> 1 model.fit(x, y,batch_size=10,validation_split=0.1)

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

~\anaconda3\lib\site-packages\keras\engine\data_adapter.py in train_validation_split(arrays, validation_split)
   1662     unsplitable = [type(t) for t in flat_arrays if not _can_split(t)]
   1663     if unsplitable:
-> 1664         raise ValueError(
   1665             "`validation_split` is only supported for Tensors or NumPy "
   1666             "arrays, found following types in the input: {}".format(unsplitable)

ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following types in the input: [<class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>]

我试图编译我的模型,但在最后一行model.fit(x, y,batch_size=10,validation_split=0.1)中发现了一个错误。

xeufq47z

xeufq47z1#

如错误中所述,在拟合模型之前,需要将输入和目标数据转换为numpy数组或tensor
可以使用以下代码将xy数据更改为数组:

import numpy as np
x=np.array(x)
y=np.array(y)

我使用kaggle的同类data复制了这段代码。
请查找随附的gist以供参考。

相关问题