简体   繁体   English

如何为深度学习设置激活或优化器 model 只接受 RNN 的 1,0

[英]How to set activation or optimiser for deeplearning model only accept 1,0 for RNN

For example例如

I have time seriese datalike this我有这样的时间序列数据

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

and it predict the next one from past two.它从过去的两个预测下一个。

I want to put [[1,0,0,0],[1,0,0,1]] and get [1,0,1,0]我想输入[[1,0,0,0],[1,0,0,1]]并得到[1,0,1,0]

So I made model like these below.所以我像下面这样制作了 model。

input_len = 2
n_in = 4
n_hidden = 512
model = Sequential()

model.add(LSTM(n_hidden, input_shape=(input_len,n_in), return_sequences=True))
        
model.add(Dropout(0.1))
model.add(LSTM(n_hidden,  return_sequences=False))
        
model.add(Dense(n_hidden, activation="linear")) 
        
model.add(Dense(n_in, activation="linear"))
opt = Adam(lr=0.001)
model.compile(loss='mse', optimizer=opt)
model.summary()

#trainning and validate data 

X     #X.shape (800, 2, 4) [ [[1,0,0,1],[1,0,0,1]],[[1,0,0,1],[1,0,0,0]],,,
Y     #Y.shape (200, 2, 4)
val_x #val_x.shape (800,1,4) [[1,0,1,0]][1,1,1,0],,,,
val_y #val_y.shape (200,1,4)

history = model.fit(x, y, epochs=50,validation_data=(val_x, val_y))

#then predict
in_ = np.array[[1,0,0,1][1,1,1,1]]
out_ = model.predict(in_)
print(out_)

I expect as the result at least 1 or 0 .我希望结果至少为10

however I get the number like this [[4.9627638e-01 1.4797167e-01 3.3314908e-01 1.3892795e-04]]但是我得到这样的数字[[4.9627638e-01 1.4797167e-01 3.3314908e-01 1.3892795e-04]]

I guess this is relevant with activation or optimizer ...我想这与activationoptimizer有关......

Am I correct?我对么? or how should I do for 1 and 0 data?或者我应该如何处理 1 和 0 数据?


change linear to relulinear更改为relu

the result becomes between [0.41842282 0.1275532 0. 0.4288069]结果变为[0.41842282 0.1275532 0. 0.4288069]

However still it is not 0 or 1....但是它仍然不是 0 或 1....

Model output can not be discrete because it should be differentiable. Model output 不能是离散的,因为它应该是可微的。 Try to add something like that:尝试添加类似的内容:

out_ = tf.cast(tf.math.greater(out_, 0.5), tf.int32)

It is not right prediction, but the accuracy depends on your data (eg if your data is random and there is no pattern - then you get 6% accuracy).这不是正确的预测,但准确性取决于您的数据(例如,如果您的数据是随机的并且没有模式 - 那么您将获得 6% 的准确性)。 Try to train based on only [[1,0,0,0] [1,0,0,1],[1,0,1,0]] to be sure that your model works.尝试仅基于[[1,0,0,0] [1,0,0,1],[1,0,1,0]]进行训练,以确保您的 model 正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM