重复 2 位 x 12 序列的训练剂量没有进展



我正在测试simpleRNN模型

我的测试数据是超级简单的重复三重2位*12

[1,0,0,0,1,0,0,1,0,0,0,0],[1,0,0,0,0,1,0,0,0,1,0,0],[0,0,1,0,0,0,0,1,0,0,0,1]

for i in range(0,100):
temp = [[1,0,0,0,1,0,0,1,0,0,0,0],
[1,0,0,0,0,1,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,0,0,1]]
line.extend(temp)
total = []
for j in range(0,500):
total.append(line)
total = np.array(total)
print(total.shape) # (500, 300, 12)

它使(500, 300, 12)变为numpy。所有的数据都是重复的,所以我希望训练和预测能完美地工作。

但CCD_ 3没有减少,预测效果不佳。

Epoch 1/5
743/743 [==============================] - 5s 5ms/step - loss: 0.1386 - val_loss: 0.1305
Epoch 2/5
743/743 [==============================] - 3s 4ms/step - loss: 0.1305 - val_loss: 0.1294
Epoch 3/5
743/743 [==============================] - 3s 4ms/step - loss: 0.1299 - val_loss: 0.1292
Epoch 4/5
743/743 [==============================] - 3s 4ms/step - loss: 0.1300 - val_loss: 0.1291
Epoch 5/5
743/743 [==============================] - 3s 4ms/step - loss: 0.1299 - val_loss: 0.1293
[[ 0.67032564 -0.0020391   0.3332582  -0.0095186   0.35370785  0.3042156
0.00809216  0.7059332   0.00199411  0.30952734 -0.0021943   0.333712  ]]
tf.Tensor([[1 0 0 0 0 0 0 1 0 0 0 0]], shape=(1, 12), dtype=int32)

我希望结果是笑脸[1,0,0,0,1,0,0,1,0,0,0,0],,,

我需要更改某个位置,否则我的代码是错误的??

这是我的全部绳索。

import tensorflow as tf
from django.core.management.base import BaseCommand, CommandError
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM,Dropout,Dense
from tensorflow.keras.layers import SimpleRNN
import numpy as np
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
def makeModel(input_len,n_in):
n_hidden = 512
model = Sequential()
model.add(SimpleRNN(n_hidden, input_shape=(input_len, n_in), return_sequences=False))
model.add(Dense(n_hidden, activation="relu")) 
model.add(Dense(n_in, activation="relu"))
opt = Adam(lr=0.001)
model.compile(loss='mse', optimizer=opt)
model.summary()
return model

line = []

for i in range(0,100):
temp = [[1,0,0,0,1,0,0,1,0,0,0,0],
[1,0,0,0,0,1,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,0,0,1]]
line.extend(temp)
total = []
for j in range(0,500):
total.append(line)
total = np.array(total)
print(total.shape) # (500, 300, 12)
chordIdList = total
n_in = 12 # dimention
input_len = 3 # length to use prediction.
model = makeModel(input_len,n_in)
input_=[]
target_=[]
for C in chordIdList:
for i in range(0, len(C) - input_len):  
input_.append( C[i:i+input_len] )  
target_.append( C[i+input_len] )  

X = np.array(input_)
Y = np.array(target_).reshape((len(input_),1,n_in))
from sklearn.model_selection import train_test_split
x, x_val,y, y_val = train_test_split(X, Y, train_size=0.8, random_state=1)
print(x.shape) # (23760, 3, 12)
print(y.shape) # (23760, 1, 12)
print(x_val.shape) #(5940, 3, 12)
print(y_val.shape) # (5940, 1, 12)
epoch = 5
history = model.fit(x, y, epochs=epoch,validation_data=(x_val, y_val))
in_ = np.array([[1,0,0,0,1,0,0,1,0,0,0,0],
[1,0,0,0,0,1,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,0,0,1]]).reshape(1,3,12) 

print(in_.shape)
out_ = model.predict(in_)
print(out_)

好吧,这里有一个主要问题,您正在尝试进行回归,而您的问题是纯粹的分类

你需要在代码的这一部分做什么:

def makeModel(input_len,n_in):
n_hidden = 512
model = Sequential()
model.add(SimpleRNN(n_hidden, input_shape=(input_len, n_in), return_sequences=False))
model.add(Dense(n_hidden, activation="relu")) 
model.add(Dense(n_in, activation="relu"))
opt = Adam(lr=0.001)
model.compile(loss='mse', optimizer=opt)
model.summary()
return model

将最后一层更改为S形激活(输出在0和1之间,就像您的情况一样(

model.add(Dense(n_in, activation="sigmoid"))

将损失更改为二进制交叉熵

model.compile(loss='binary_crossentropy', optimizer=opt)

通过使用relu,你试图将值映射到一个无限函数,这会使学习变得复杂。

此外,使用挤压Y

history = model.fit(x, np.squeeze(y), epochs=epoch,validation_data=(x_val, np.squeeze(y_val)))

相关内容

  • 没有找到相关文章

最新更新