Tens中的logits和labels

2024-05-17 13:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我对开发神经网络相当陌生,一直试图构建一个基本的LSTM分类器,但遇到了一个错误:“logits和labels必须具有相同的第一维度,got logits shape[125,3]和labels shape[25]”

我使用了相同的数据转换函数和数据形状,并将几乎相同的神经网络体系结构应用于回归LSTM,但没有遇到这个问题。架构上的主要区别在于,回归模型的输出是密集的,只有1个输出

下面是我的示例代码,从数据预处理和转换到模型的体系结构和拟合

#scale the input variables
sc = MinMaxScaler(feature_range=(0,1))
#get the data examples
inputs = sc.fit_transform(future[['close', 'bullprice', 'bearprice', 'RSI', 'bullrsi', 
                                  'bearrsi', 'bulldiv', 'beardiv', 'com_hedgers', 
                                  'bullhedge', 'bearhedge']])
labels = future[['indicator']].values
data = np.concatenate((inputs, labels), axis=1)
data = data[14:] #get rid of first 14 rows because of RSI 

#function to convert examples into LSTM input/X (needs to be 3D matrix) and labels (output/Y)
def create_dataset(dataset, look_back): #changed lookback here
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back - 1):
        a = dataset[i:(i + look_back), :-1]
        dataX.append(a)
        dataY.append(dataset[i + look_back, -1])
    return np.array(dataX), np.array(dataY)
look_back = 5 #looking back n days to make a the next days prediction, UNDERSTAND THIS AND EXPLAIN CONCISELY
num_features = 11 #place the number of features that are going in to the model

#apply function to the data
X, Y = create_dataset(data, look_back) 

#split into train and test set data
trainX, testX = np.split(X, [int(.7*len(X))])
trainY, testY = np.split(Y, [int(.7*len(Y))])
trainY = trainY+1
testY = testY+1

# one-hot encode the outputs (sell, hold, buy) DOES NOT SEEM TO WORK FOR THE MODEL SO I COMMENTED IT OUT
#onehot_encoder = OneHotEncoder(categories='auto', sparse=False) #'categories=auto' allows the negative categories to work
#trainY = onehot_encoder.fit_transform(trainY.reshape([-1, 1])) #one-hot encoded training set
#testY = onehot_encoder.fit_transform(testY.reshape([-1, 1])) #one-hot encoded test set

'''
Building and fitting the deep learning model
'''
model = tf.keras.Sequential([
    tf.keras.layers.LSTM(trainX.shape[0], input_shape=(look_back,num_features), return_sequences=True), #gives 128 units and outputs them to the next layer
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.LSTM(128, return_sequences=True),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(3,  activation=tf.nn.softmax) #softmax gives us a probability distribution
])
model.compile(optimizer='RMSProp', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(trainX, trainY, validation_split=0.2, epochs=3, batch_size=25) 
#the first dimension of logits shape is a function of the size of the batch_size and look_back
#something is wrong with the shaping of the data

我的问题与logits输出的第一个维度有关,即“look\u back”变量(在代码的第一部分)和模型拟合中的批大小。这是基于矩阵的所有维度-只是不知道如何确定修复

我希望得到正确运行的模型,然后能够输出三类预测的概率分布

谢谢你的时间。请让我知道什么澄清和进一步的信息,你可能需要


Tags: andofthetodatalabelstfnp