我是python和ML,Tensor流的初学者,使用资源练习与它们的玩法;在我的一个课堂项目中使用基于Olivetti数据集的人脸识别;但在给出以下命令分类器编码的最后一行[tensorflow和keras之后(使用ANN)之后),却无法找出重新整形数据的错误
我已经获取了.npy文件和一个包含图像的输入数据文件。对象的采样图像。由于图像数据为矩阵形式,因此必须将其转换为矢量。分割数据用于训练和测试,然后通过Google colab和keras使用张量流。 ''''
>!pip install tensorflow
>import tensorflow as tf
>!pip install tensorflow_gpu
>from google.colab import files
>file = files.upload() # data is uploaded olivetti_faces.npy and
>file = files.upload() # data is uploaded olivetti_faces_target.npy
>import numpy as np
>import pandas as pd
>import matplotlib.pyplot as plt
>from skimage.io import imshow
>import matplotlib.image as mpimg
>%matplotlib inline
>pics = np.load("olivetti_faces.npy")
>labels = np.load("olivetti_faces_target.npy")
>print("pics: ", pics.shape)
>print("labels: ", labels.shape)
>img_cnt = 10
>plt.figure(figsize=(24,24))
>for i in range(img_cnt):
>plt.subplot(1,10,i+1)
>x=pics[i+40] # 5th subject
>imshow(x)
>plt.show()
>Y = labels.reshape(-1,1) # store labels in Y
>X=pics.reshape(pics.shape[0], pics.shape[1]*pics.shape[2])
>print("X shape:",X.shape)
>print("Y shape:",Y.shape)
>from sklearn.model_selection import train_test_split
>x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.1, random_state=46)
>!pip install keras
>from keras.models import Sequential
>classifier = Sequential()
>classifier.add(Dense(units=16,activation='relu',input_dim=10))
>classifier.add(Dense(units=6,activation='relu'))
>classifier.add(Dense(units=1,activation='relu') >classifier.compile(optimizer='rmsprop',loss='binary_crossentropy')
>classifier.fit(x_train,y_train,batch_size=1,epochs=10)
##This is error
>ValueError Traceback (most recent call last)
>ipython-input-59-e087468b69e1> in <module>()
>1 classifier.fit(x_train,y_train,batch_size=1,epochs=1000)
>2 frames
> /usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py
>in standardize_input_data(data, names, shapes, check_batch_axis,
>exception_prefix)
> 136 ': expected ' + names[i] + ' to
>have shape ' +
> 137 str(shape) + ' but got array
>with shape ' > 138 str(data_shape))
> 139 return data
> 140
> ValueError: Error when checking input: expected dense_1_input to have
>shape (10,) but got array with shape (4096,)
''''