我尝试实现前馈神经网络。
这是结构: 输入层:8个神经元,隐藏层:8个神经元和输出层:8个神经元。
输入数据是8位的向量(输入层的每个神经元为1位)。 神经网络的输出也是8位的向量。所以总共数据集有256个例子。
示例:如果给定x = [0.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0]
输出必须是y = [1.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0]
这是实施:
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import random
from math import ceil
#Dimension of layers
dim = 8
#Generate dataset
X = []
for i in range(0,2**dim):
n = [float(x) for x in bin(i)[2:]]
X.append([0.]*(dim-len(n))+n)
y = X[:]
random.shuffle(y)
X = np.array(X)
y = np.array(y)
# create model
model = Sequential()
model.add(Dense(dim, input_dim=dim, init='normal', activation='sigmoid'))
model.add(Dense(dim, init='normal', activation='sigmoid'))
model.add(Dense(dim, init='normal', activation='sigmoid'))
# Compile model
model.compile(loss='mse', optimizer='SGD', metrics=['accuracy'])
# Fit the model
model.fit(X, y, nb_epoch=1000, batch_size=50, verbose=0)
# evaluate the model
scores = model.evaluate(X, y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
output = model.predict(X)
#Make the output binary
for i in range(0, output[:,0].size):
for j in range(0, output[0].size):
if output[i][j] > 0.5 or output[i][j] == 0.5:
output[i][j] = 1
else:
output[i][j] = 0
print(output)
这是我输出的内容:
acc: 50.39%
[[ 1. 0. 0. ..., 0. 1. 1.]
[ 1. 0. 0. ..., 0. 1. 1.]
[ 1. 0. 0. ..., 0. 1. 1.]
...,
[ 1. 0. 0. ..., 0. 1. 1.]
[ 1. 0. 0. ..., 0. 1. 1.]
[ 1. 0. 0. ..., 0. 1. 1.]]
似乎所有输出都具有相同的值。所以我不知道配置有什么问题。我尝试了这个Cannot train a neural network in keras - stackoverflow,它建议删除输出层的激活函数,但是当我运行它时,我得到所有具有此值的输出向量:
[0.1 1. 1. ...,1.1.1。]
有关如何使其有效的任何见解?
答案 0 :(得分:10)
我遇到了同样的问题。
我建议你降低SGD的学习率。在我的情况下,我使用了Adam Optimizer,lr = 0.001,但是改为0.0001解决了这个问题。
SGD are的默认参数:
keras.optimizers.SGD(lr = 0.01,动量= 0.0,衰减= 0.0,nesterov = False)
答案 1 :(得分:7)
输出与多标签分类相似,所以我建议:
答案 2 :(得分:1)
如果你尝试了以上所有内容并且它不起作用就意味着 如果您尝试适应噪声,则输入和输出之间没有连接/相关/相关性。