keras网络无法训练

v2g6jxz6  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(346)

我第一次尝试做最简单的网。我为xor训练它。它不是绝对有效的。尝试每一项:不同的激活功能,层数,神经元,时间,批次,优化。。。每次结果为1,1,1,1(精度=0.5)。求求你,救命!我做错了什么?

from keras.models import Sequential
from keras.layers import Dense
from tensorflow import keras
import numpy as np

X = np.array([  [0,0],
                [0,1],
                [1,0],
                [1,1] ])
Y = np.array([[1,0,0,1]]).T

model = Sequential()
model.add(Dense(10, input_dim=2, activation='relu'))
model.add(Dense(10, activation='relu'))  
model.add(Dense(1, activation='softmax')) 

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics='accuracy')

# Traiting a model

model.fit(X, Y, epochs=100, batch_size=len(X))

# Prediction

predictions = model.predict(X)
print(predictions)

我注意到,在输出的左侧总是有1/1。但是,我想,一定有类似4/4的东西。这可能是原因吗?但我不明白怎么修复它。。。
输出的尾部:

...
...
Epoch 97/100
1/1 [==============================] - 0s 1ms/step - loss: 0.0000e+00 - accuracy: 0.5000
Epoch 98/100
1/1 [==============================] - 0s 1ms/step - loss: 0.0000e+00 - accuracy: 0.5000
Epoch 99/100
1/1 [==============================] - 0s 1ms/step - loss: 0.0000e+00 - accuracy: 0.5000
Epoch 100/100
1/1 [==============================] - 0s 1ms/step - loss: 0.0000e+00 - accuracy: 0.5000
1/1 [==============================] - 0s 165ms/step - loss: 0.0000e+00 - accuracy: 0.5000
[0.0, 0.5]
[[1.]
[1.]
[1.]
[1.]]
pvabu6sv

pvabu6sv1#

非常感谢大家!
下面是工作网。奇怪,训练时间太长了!我记得,几年前我做了同样的任务,但没有keras。培训几乎是即时的(当然没有任何gpu)。但这里的“亚当优化”(使用“fast relu”,我只完成了4层网络)。似乎对于这样简单的任务,函数具有相反的效果。

from keras.models import Sequential
from keras import initializers

from keras.layers import Dense
from tensorflow import keras
import numpy as np

X = np.array([0,0,
              0,1,
              1,0,
              1,1] )

X = X.reshape(4,2).astype("float32")

Y = np.array([1,
              0,
              0,
              1] )
Y = Y.reshape(4,1).astype("float32")

init_2 = initializers.TruncatedNormal(mean=0.0, stddev=0.05, seed=12345)

model = Sequential()
model.add(Dense(4, input_dim=2, activation='sigmoid', kernel_initializer=init_2, bias_initializer=init_2))
model.add(Dense(1, activation='sigmoid', kernel_initializer=init_2, bias_initializer=init_2)) 

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Traiting a model

model.fit(X, Y, epochs=7000, batch_size=4, verbose=0)

scores = model.evaluate(X, Y)
print(scores)

# Prediction

predictions = model.predict(X)
print(predictions)

相关问题