Skip to content

Instantly share code, notes, and snippets.

@braingineer
Created April 20, 2016 15:10
Show Gist options
  • Save braingineer/727f9b3d7fbcd8a0f0d8cd1232d24c29 to your computer and use it in GitHub Desktop.
Save braingineer/727f9b3d7fbcd8a0f0d8cd1232d24c29 to your computer and use it in GitHub Desktop.
Dense layer being rebuilt by TimeDistributed
from __future__ import print_function
from keras.layers import Dense, TimeDistributed
import numpy as np
import theano
def shared(x):
x_ = theano.shared(x)
x_._keras_shape = x.shape
return x_
dense_func = Dense(1)
dense_func.build((1,5))
d1 = shared(np.arange(20).reshape(1,4,5).astype(np.float32))
print("Dense function is built:", dense_func.built)
for i in range(2):
print("round", i)
print("="*20)
print("Dense function is built:", dense_func.built)
print("-"*10)
print("dense layer weights:\n\t", np.array(dense_func.W.eval()).flatten())
print("-"*10)
o1 = TimeDistributed(dense_func)(d1).eval().flatten()
o2 = TimeDistributed(dense_func)(d1).eval().flatten()
print("out1:\n\t", o1)
print("out2:\n\t", o2)
print("With marking..")
dense_func = Dense(1)
dense_func.build((1,5))
dense_func.built = True
d1 = shared(np.arange(20).reshape(1,4,5).astype(np.float32))
print("Dense function is built:", dense_func.built)
for i in range(2):
print("round", i)
print("="*20)
print("Dense function is built:", dense_func.built)
print("-"*10)
print("dense layer weights:\n\t", np.array(dense_func.W.eval()).flatten())
print("-"*10)
o1 = TimeDistributed(dense_func)(d1).eval().flatten()
o2 = TimeDistributed(dense_func)(d1).eval().flatten()
print("out1:\n\t", o1)
print("out2:\n\t", o2)
"""
OUTPUT:
Using Theano backend.
Using gpu device 0: GeForce GTX 980 (CNMeM is disabled, CuDNN 3007)
Dense function is built: False
round 0
====================
Dense function is built: False
----------
dense layer weights:
[-0.16217758 -0.51210076 0.93307728 0.96743965 0.14936669]
----------
out1:
[ 0.00654554 0.54339099 1.08023643 1.61708248]
out2:
[ -1.07750547 -5.27540827 -9.47331142 -13.6712141 ]
round 1
====================
Dense function is built: False
----------
dense layer weights:
[-0.44987136 -0.24459462 0.08279426 0.08686388 -0.31477275]
----------
out1:
[ 2.91354108 14.76345634 26.6133728 38.46329117]
out2:
[ 4.15778255 10.96348095 17.76917839 24.57487679]
With marking..
Dense function is built: True
round 0
====================
Dense function is built: True
----------
dense layer weights:
[ 0.22700137 0.43190205 0.57331359 -0.61278355 -0.56270558]
----------
out1:
[ -5.25695324 -18.86313438 -32.46931458 -46.07549286]
out2:
[ 0.62759721 3.35421753 6.0808382 8.80745888]
round 1
====================
Dense function is built: True
----------
dense layer weights:
[ 0.21340257 0.11011547 0.6671744 -0.9646064 0.51923805]
----------
out1:
[ -1.90755129 -4.99689198 -8.08623314 -11.17557335]
out2:
[ -3.08132529 -7.85123491 -12.62114429 -17.39105415]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment