2017-02-11 6 views
8

के साथ तंत्रिका नेटवर्क में डेटा की मैन्युअल रूप से भविष्यवाणी करने के लिए वजन का उपयोग कैसे करें मैंने तंत्रिका नेटवर्क की संरचना के संबंध में ऑनलाइन गाइड का पालन करने के लिए अपना सर्वश्रेष्ठ प्रयास किया है, लेकिन मुझे कुछ मौलिक याद आना चाहिए। प्रशिक्षित वजन के एक सेट को उनके पूर्वाग्रह के साथ देखते हुए, मैं भविष्यवाणी विधि का उपयोग किये बिना उन वजनों के साथ मैन्युअल रूप से इनपुट की भविष्यवाणी करना चाहता हूं।करास

कैमरों के साथ एमएनआईएसटी छवियों का उपयोग करके मैंने पूर्वाग्रह के लिए एक अतिरिक्त सुविधा शामिल करने के लिए मैन्युअल रूप से अपना डेटा संपादित करने का प्रयास किया है, हालांकि यह प्रयास कोई पूर्वाग्रह का उपयोग करने की तुलना में कोई बेहतर छवि सटीकता प्रदान नहीं करता है, और निश्चित रूप से बहुत कम सटीकता कैरेस भविष्यवाणी विधि का उपयोग कर। मेरा कोड मेरे प्रयास के साथ नीचे है।

कृपया सटीक छवि प्रतिनिधित्व के लिए कैरस विधि भविष्यवाणी का उपयोग करने के लिए नीचे की दो टिप्पणियां नोट करें, और फिर वजन कम करने और पूर्वाग्रह जोड़ने से मेरा खराब प्रयास।

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 

उत्तर

5

मैनुअल भविष्यवाणी गणना पहली परत और activation='sigmoid' अंतिम परत में के बाद activation='relu' की तरह गायब सक्रियण समारोह से सही अन्य हो रहा है।

ठीक काम करना चाहिए मैनुअल भविष्यवाणी कोड और भविष्यवाणी में निम्न परिवर्तन करें:

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) 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^