python - What's the format of keras' matrix multiplication -


all math i've seen behind propagating data 1 neural network layer next in calculating z looks like:

z = θtx+b

but keras seems diverge standard. accepts input matrices in form of number of samples rows , number of features columns , get_weights() command returns matrices shapes satisfy equation z if following true:

z = xθ+b

given following example of network learning xor gate input dimensions 4x2 , output dimensions 4x1:

from keras.models import sequential keras.layers import dense import numpy np   x = np.array([[0,0],               [1,0],               [0,1],               [1,1]]) y = np.array([0,1,1,0])  model = sequential() model.add(dense(10, input_dim=2, activation='sigmoid')) model.add(dense(10, activation='sigmoid')) model.add(dense(1, activation='sigmoid'))  model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy']) model.fit(x, y, epochs=100000, batch_size=4, verbose=0)  print model.get_weights() print model.predict(x) 

the model weights each layer come out 2x10, 10x10, , 10x1. matrix multiplication fails satisfy first equation given z appears work second. keras handle neural network computations way or misinterpreting in code somewhere? should input x dimensions transposed instead? appreciated.

there problem way setup weights (shape). take @ example, taken here:

from keras.models import sequential keras.layers.core import dense, dropout, activation keras.optimizers import sgd import numpy np   x = np.array([[0,0],[0,1],[1,0],[1,1]]) y = np.array([[0],[1],[1],[0]])  model = sequential() model.add(dense(8, input_dim=2)) model.add(activation('tanh')) model.add(dense(1)) model.add(activation('sigmoid'))  sgd = sgd(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd)  model.fit(x, y, show_accuracy=true, batch_size=1, nb_epoch=1000) print(model.predict_proba(x)) """ [[ 0.0033028 ]  [ 0.99581173]  [ 0.99530098]  [ 0.00564186]] """ 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -