如何使用权重手动预测神经网络中的数据与karas

时间:2017-02-11 17:54:23

标签: neural-network keras

我已尽力遵循有关神经网络结构的在线指南,但我必须遗漏一些基本的东西。给定一组经过训练的权重及其偏差,我想简单地使用这些权重手动预测输入,而不使用预测方法。

使用带有keras的MNIST图像我试图手动编辑我的数据以包含偏差的额外功能,但是这种努力似乎没有提供比没有偏差更好的图像精确度,并且绝对精度要低得多比使用keras预测方法。我的代码在我的尝试下面。

请注意底部附近的两条评论,用于使用keras方法预测来获得准确的图像表示,然后我很难尝试手动获取权重并添加偏差。

from keras.datasets import mnist
import numpy as np
import time
from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf
from matplotlib import pyplot as plt

comptime=time.time()
with tf.device('/cpu:0'):
    tf.placeholder(tf.float32, shape=(None, 20, 64))

    seed = 7
    np.random.seed(seed)
    model = Sequential()
    (x_train, _), (x_test, _) = mnist.load_data()
    x_train = x_train.astype('float32') / 255.
    priorShape_x_train=x_train.shape #prior shape of training set
    x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
    x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
    x_train_shaped=x_train
    model.add(Dense(32, input_dim=784, init='uniform', activation='relu'))
    model.add(Dense(784, init='uniform', activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=['accuracy'])
    model.fit(x_train[1:2500], x_train[1:2500], nb_epoch=10)

#proper keras prediction
prediction_real=model.predict(x_train[57:58])
prediction_real=prediction_real.reshape((28,28))

#manual weight prediction attempt
x_train=np.hstack([x_train,np.zeros(x_train.shape[0]).reshape(x_train.shape[0],1)]) #add extra column for bias
x_train[:,-1]=1 #add placeholder as 1
weights=np.vstack([model.get_weights()[0],model.get_weights()[1]]) #add trained weights as extra row vector
prediction=np.dot(x_train,weights) #now take dot product.. repeat pattern for next layer
prediction=np.hstack([prediction,np.zeros(prediction.shape[0]).reshape(prediction.shape[0],1)])
prediction[:,-1]=1
weights=np.vstack([model.get_weights()[2],model.get_weights()[3]])
prediction=np.dot(prediction,weights)
prediction=prediction.reshape(priorShape_x_train)

plt.imshow(prediction[57], interpolation='nearest',cmap='gray')
plt.savefig('myprediction.png') #my prediction, not accurate
plt.imshow(prediction_real,interpolation='nearest',cmap='gray')
plt.savefig('realprediction.png') #in-built keras method, accurate

1 个答案:

答案 0 :(得分:11)

手动预测计算似乎是正确的,除了缺少激活函数,如第一层后的activation='relu'和最后一层中的activation='sigmoid'

对手动预测代码进行以下更改,预测应该可以正常工作:

from scipy.stats import logistic

weights=np.vstack([model.get_weights()[0],model.get_weights()[1]]) 
prediction=np.dot(x_train,weights) 

prediction[prediction<0]=0              ### RELU after 1st layer

prediction=np.hstack([prediction,np.zeros(prediction.shape[0]).reshape(prediction.shape[0],1)])
prediction[:,-1]=1
weights=np.vstack([model.get_weights()[2],model.get_weights()[3]])
prediction=np.dot(prediction,weights)

prediction=logistic.cdf(prediction)     ### Sigmoid after 2nd layer

prediction=prediction.reshape(priorShape_x_train)