SHARE
TWEET

Untitled

a guest Mar 20th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import keras
  2. from keras.layers import *
  3. from keras.models import Model
  4. import theano as T
  5. import tensorflow as tf
  6. print('theano ver.',T.__version__)
  7. print('tensorflow ver.',tf.__version__)
  8. print('keras ver.',keras.__version__)
  9. np.set_printoptions(precision=4)
  10. np.random.seed(1)
  11. # had same results with both tf and T
  12. from keras.models import Sequential
  13.  
  14. def sequential_non_temporal_example():
  15.     model = Sequential()
  16.     model.add(Masking(mask_value = mask_value,input_shape=(n_timesteps, n_features)))
  17.     model.add(Dense(1,activation='linear',kernel_initializer="one"))
  18.     model.summary()
  19.  
  20.     output_val = model.predict(data)
  21.  
  22.     print '--> time'
  23.     print np.linspace(0,n_timesteps-1,n_timesteps)
  24.     for s in xrange(n_samples):
  25.         print '# sample = ',s
  26.         print 'input:'
  27.         print(data[s,:,:].T)
  28.         print 'output_val:'
  29.         print(output_val[s,:,:].T)
  30.  
  31. def sequential_temporal_example():
  32.     model = Sequential()
  33.     model.add(Masking(mask_value = mask_value,input_shape=(n_timesteps, n_features)))
  34.     model.add(LSTM(2, return_sequences=True, kernel_initializer="one"))
  35.     model.add(Dense(1,activation='linear',kernel_initializer="one"))
  36.    
  37.     model.summary()
  38.  
  39.     output_val = model.predict(data)
  40.  
  41.     print '--> time'
  42.     print np.linspace(0,n_timesteps-1,n_timesteps)
  43.     for s in xrange(n_samples):
  44.         print '# sample = ',s
  45. import keras
  46. from keras.layers import *
  47. from keras.models import Model
  48. import theano as T
  49. import tensorflow as tf
  50. print('theano ver.',T.__version__)
  51. print('tensorflow ver.',tf.__version__)
  52. print('keras ver.',keras.__version__)
  53. np.set_printoptions(precision=4)
  54. np.random.seed(1)
  55.  
  56. from keras.models import Sequential
  57.  
  58. def sequential_non_temporal_example():
  59.     model = Sequential()
  60.     model.add(Masking(mask_value = mask_value,input_shape=(n_timesteps, n_features)))
  61.     model.add(Dense(1,activation='linear',kernel_initializer="one"))
  62.     model.summary()
  63.  
  64.     output_val = model.predict(data)
  65.  
  66.     print '--> time'
  67.     print np.linspace(0,n_timesteps-1,n_timesteps)
  68.     for s in xrange(n_samples):
  69.         print '# sample = ',s
  70.         print 'input:'
  71.         print(data[s,:,:].T)
  72.         print 'output_val:'
  73.         print(output_val[s,:,:].T)
  74.  
  75. def sequential_temporal_example():
  76.     model = Sequential()
  77.     model.add(Masking(mask_value = mask_value,input_shape=(n_timesteps, n_features)))
  78.     model.add(LSTM(2, return_sequences=True, kernel_initializer="one"))
  79.     model.add(Dense(1,activation='linear',kernel_initializer="one"))
  80.    
  81.     model.summary()
  82.  
  83.     output_val = model.predict(data)
  84.  
  85. import keras
  86. from keras.layers import *
  87. from keras.models import Model
  88. import theano as T
  89. import tensorflow as tf
  90. print('theano ver.',T.__version__)
  91. print('tensorflow ver.',tf.__version__)
  92. print('keras ver.',keras.__version__)
  93. np.set_printoptions(precision=4)
  94. np.random.seed(1)
  95.  
  96. from keras.models import Sequential
  97.  
  98. def sequential_non_temporal_example():
  99.     model = Sequential()
  100.     model.add(Masking(mask_value = mask_value,input_shape=(n_timesteps, n_features)))
  101.     model.add(Dense(1,activation='linear',kernel_initializer="one"))
  102.     model.summary()
  103.  
  104.     output_val = model.predict(data)
  105.  
  106.     print '--> time'
  107.     print np.linspace(0,n_timesteps-1,n_timesteps)
  108.     for s in xrange(n_samples):
  109.         print '# sample = ',s
  110.         print 'input:'
  111.         print(data[s,:,:].T)
  112.         print 'output_val:'
  113.         print(output_val[s,:,:].T)
  114.  
  115. def sequential_temporal_example():
  116.     model = Sequential()
  117.     model.add(Masking(mask_value = mask_value,input_shape=(n_timesteps, n_features)))
  118.     model.add(LSTM(2, return_sequences=True, kernel_initializer="one"))
  119.     model.add(Dense(1,activation='linear',kernel_initializer="one"))
  120.    
  121.     model.summary()
  122.  
  123.     output_val = model.predict(data)
  124.  
  125.     print '--> time'
  126.     print np.linspace(0,n_timesteps-1,n_timesteps)
  127.     for s in xrange(n_samples):
  128.         print '# sample = ',s
  129.         print 'input:'
  130.         print(data[s,:,:].T)
  131.         print 'output_val:'
  132.         print(output_val[s,:,:].T)
  133.    
  134.    
  135. n_samples  = 3
  136. n_timesteps = 7
  137. n_features = 2
  138. mask_value = np.nan # -999999999.0# -1.0 # -1 # 0.0
  139. data = np.ones((n_samples,n_timesteps,n_features))
  140.  
  141. for s in xrange(n_samples):
  142.     for f in xrange(n_features):
  143.         data[s,:,f] = np.linspace(1,n_timesteps,n_timesteps)
  144.  
  145. # mask a feature value of one sample and timestep (no effect)
  146. data[1,0,0] = mask_value
  147.  
  148. # mask all feature values of one sample and timestep (propagates 0.*mask_value at layer of step/sample?)
  149. data[2,3,:] = mask_value
  150.  
  151. print '####################### sequential_non_temporal_example #######################:'
  152. sequential_non_temporal_example()
  153. print '####################### sequential_temporal_example #######################:'
  154. # As non-temporal but masked timestep state does not propagate through time:
  155. sequential_temporal_example()
  156.  
  157. # ('theano ver.', '0.8.2')
  158. # ('tensorflow ver.', '1.0.1')
  159. # ('keras ver.', '2.0.1')
  160. # ####################### sequential_non_temporal_example #######################:
  161. # _________________________________________________________________
  162. # Layer (type)                 Output Shape              Param #  
  163. # =================================================================
  164. # masking_1 (Masking)          (None, 7, 2)              0        
  165. # _________________________________________________________________
  166. # dense_1 (Dense)              (None, 7, 1)              3        
  167. # =================================================================
  168. # Total params: 3.0
  169. # Trainable params: 3.0
  170. # Non-trainable params: 0.0
  171. # _________________________________________________________________
  172. # --> time
  173. # [ 0.  1.  2.  3.  4.  5.  6.]
  174. # # sample =  0
  175. # input:
  176. # [[ 1.  2.  3.  4.  5.  6.  7.]
  177. #  [ 1.  2.  3.  4.  5.  6.  7.]]
  178. # output_val:
  179. # [[  2.   4.   6.   8.  10.  12.  14.]]
  180. # # sample =  1
  181. # input:
  182. # [[ nan   2.   3.   4.   5.   6.   7.]
  183. #  [  1.   2.   3.   4.   5.   6.   7.]]
  184. # output_val:
  185. # [[ nan   4.   6.   8.  10.  12.  14.]]
  186. # # sample =  2
  187. # input:
  188. # [[  1.   2.   3.  nan   5.   6.   7.]
  189. #  [  1.   2.   3.  nan   5.   6.   7.]]
  190. # output_val:
  191. # [[  2.   4.   6.  nan  10.  12.  14.]]
  192. # ####################### sequential_temporal_example #######################:
  193. # _________________________________________________________________
  194. # Layer (type)                 Output Shape              Param #  
  195. # =================================================================
  196. # masking_2 (Masking)          (None, 7, 2)              0        
  197. # _________________________________________________________________
  198. # lstm_1 (LSTM)                (None, 7, 2)              40        
  199. # _________________________________________________________________
  200. # dense_2 (Dense)              (None, 7, 1)              3        
  201. # =================================================================
  202. # Total params: 43.0
  203. # Trainable params: 43.0
  204. # Non-trainable params: 0.0
  205. # _________________________________________________________________
  206. # --> time
  207. # [ 0.  1.  2.  3.  4.  5.  6.]
  208. # # sample =  0
  209. # input:
  210. # [[ 1.  2.  3.  4.  5.  6.  7.]
  211. #  [ 1.  2.  3.  4.  5.  6.  7.]]
  212. # output_val:
  213. # [[ 1.2603  1.9066  1.9871  1.9982  1.9998  2.      2.    ]]
  214. # # sample =  1
  215. # input:
  216. # [[ nan   2.   3.   4.   5.   6.   7.]
  217. #  [  1.   2.   3.   4.   5.   6.   7.]]
  218. # output_val:
  219. # [[ nan  nan  nan  nan  nan  nan  nan]]
  220. # # sample =  2
  221. # input:
  222. # [[  1.   2.   3.  nan   5.   6.   7.]
  223. #  [  1.   2.   3.  nan   5.   6.   7.]]
  224. # output_val:
  225. # [[ 1.2603  1.9066  1.9871     nan     nan     nan     nan]]
RAW Paste Data
Top