Advertisement
furas

Python - keras

Jun 9th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. import keras as ks
  2. import numpy as np
  3.  
  4. features = np.zeros((1,13,22))
  5. print('features:', features.shape)
  6.  
  7. input_size = features.shape[1:]   #input_size (13, 22)
  8. print('input_size:', input_size)
  9.  
  10. # input layer
  11. input_layer = ks.layers.Input(shape=(*input_size,), name='input')
  12. print('input:', input_layer.shape) # (?, 13, 22)
  13.  
  14. # noise
  15. x = ks.layers.GaussianNoise(stddev=0.1)(input_layer)  
  16. print('1', x.shape) # (?, 13, 22)
  17.  
  18. # conv layer
  19. x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)
  20. print('2', x.shape) # (?, 13, 8)
  21.  
  22. x = ks.layers.ZeroPadding1D(padding=(1, 0))(x)
  23. print('3', x.shape) # (?, 14, 8)
  24.  
  25. x = ks.layers.MaxPool1D(pool_size=2, strides=None)(x)
  26. print('4', x.shape) # (?, 7, 8)
  27.  
  28. x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)
  29. print('5', x.shape) # (?, 7, 8)
  30.  
  31. x = ks.layers.UpSampling1D(size=2)(x)
  32. print('6', x.shape) # (?, 14, 8)
  33.  
  34. x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)
  35. print('7', x.shape) # (?, 14, 8)
  36.  
  37. x = ks.layers.Conv1D(filters=input_size[-1], kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)
  38. print('8', x.shape) # (?, 14, 22)
  39.  
  40. # output
  41. output_layer = x
  42. print('output', output_layer.shape)
  43.  
  44. model = ks.models.Model(inputs=input_layer, outputs=output_layer)
  45. model.compile(optimizer='rmsprop',
  46.               loss='categorical_crossentropy',
  47.               metrics=['accuracy'])
  48.  
  49. print(model.summary())
  50.  
  51. labels = np.zeros((1,13,22)) # gives error
  52. #labels = np.zeros((1,14,22)) # works correctly
  53. model.fit(features, labels)  # <--- # ValueError: Error when checking target: expected conv1d_4 to have shape (14, 22) but got array with shape (13, 22)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement