我正在研究Kaggle的fer2013数据集。这是数据集的a link。
我使用TFLearn框架,我将标签(7个类标签)转换为hot_shot,一切正常,直到我在网络中运行它并得到错误:无法提供形状值(64 ,7)对于Tensor'目标/ Y:0',它有形状'(?,)'
我读过以前类似的问题,我知道我试图为网络提供一个与预期不同的张量形状,我的问题是我不知道如何重塑它的形状期待,或者至少它预期的形状,所以我可以重塑我的张量。
这是我的代码。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
#Read csv file
data = pd.read_csv('fer2013.csv')
#Number of samples
n_samples = len(data)
n_samples_train = 28709
n_samples_test = 3589
n_samples_validation = 3589
IMG_SIZE = 48
#Pixel width and height
w = 48
h = 48
#Separating labels and features respectively
y = data['emotion']
X = np.zeros((n_samples, w, h,1))
for i in range(n_samples):
X[i] = np.fromstring(data['pixels'][i], dtype=int, sep=' ').reshape(w, h,1)
#Training set
X_train = X[:n_samples_train]
y_train = y[:n_samples_train]
X_val = X[n_samples_train : (n_samples_train + n_samples_test)]
y_val = y[n_samples_train : (n_samples_train + n_samples_test)]
n_values = np.max(y_train) + 1
y_hot_shot_train = np.eye(n_values)[y_train]
n_values_val = np.max(y_val) + 1
y_hot_shot_val = np.eye(n_values_val)[y_val]
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tflearn.data_augmentation import ImageAugmentation
LR = 0.001
imgaug = ImageAugmentation()
imgaug.add_random_flip_leftright()
imgaug.add_random_rotation(max_angle=25.)
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1],data_augmentation=imgaug, name='input')
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 7, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets', to_one_hot = True, n_classes=7)
model = tflearn.DNN(convnet, tensorboard_dir='log')
MODEL_NAME = 'SentimentAnalysis-{}-{}.model'.format(LR, '6conv-basic')
model.fit({'input': X_train}, {'targets': y_hot_shot_train}, n_epoch=6,batch_size=64, validation_set=({'input': X_val}, {'targets': y_hot_shot_val}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
答案 0 :(得分:0)
在回归功能中将to_one_hot
设置为True
时,它已将目标转换为单热标签。因此,它需要一个具有形状(?,)的值,您应该只将原始数据y_train
和y_val
提供给拟合函数。
model.fit({'input': X_train}, {'targets': y_train}, n_epoch=6, batch_size=64,
validation_set=({'input': X_val}, {'targets': y_val}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
答案 1 :(得分:0)
我刚刚输入上面的答案,请检查example。同时将热矩阵转换为:
--forceExit
和
num_classes = y_train.max()+1
y_hot_shot_train = np.zeros((n_samples_train,num_classes),dtype=int)
y_hot_shot_train[np.arange(n_samples_train), np.transpose(y_train)] = 1
y_hot_shot_val = np.zeros((n_samples_validation,num_classes),dtype=int)
y_hot_shot_val[np.arange(n_samples_validation), np.transpose(y_val)] = 1