keras_high_level.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding:utf-8 -*-
  2. import keras
  3. from keras.datasets import mnist
  4. from tflearn.layers.core import fully_connected
  5. from keras.layers import Input, Dense, Conv2D, MaxPooling2D
  6. from keras.models import Model
  7. def self_defined_fc():
  8. (trainX, trainY), (testX, testY) = mnist.load_data()
  9. trainX = trainX.reshape(trainX.shape[0], 784)
  10. testX = testX.reshape(testX.shape[0], 784)
  11. # int to float
  12. trainX = trainX.astype('float32')
  13. testX = testX.astype('float32')
  14. trainX /= 255.0
  15. testX /= 255.0
  16. # one-hot encode
  17. trainY = keras.utils.to_categorical(trainY, 10)
  18. testY = keras.utils.to_categorical(testY, 10)
  19. # define input
  20. inputs = Input(shape=(784,))
  21. x = Dense(500, activation='relu')(inputs)
  22. predictions = Dense(10, activation='softmax')(x)
  23. # define model
  24. model = Model(inputs=inputs, outputs=predictions)
  25. model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD(), metrics=['accuracy'])
  26. model.fit(trainX, trainY, batch_size=128, epochs=20, validation_data=(testX, testY))
  27. score = model.evaluate(testX, testY)
  28. print('Test loss: ', score[0])
  29. print('Test accuracy: ', score[1])
  30. def inception_implement():
  31. input_img = Input(shape=(256, 256, 3))
  32. # branch 1
  33. tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
  34. tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
  35. # branch 2
  36. tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
  37. tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
  38. # branch 3
  39. tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
  40. tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
  41. output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
  42. # input1(784)
  43. # |
  44. # x(1) input2(10)
  45. # | \ |
  46. # output1(10) output2(10)
  47. def multi_io_structure():
  48. (trainX, trainY), (testX, testY) = mnist.load_data()
  49. trainX = trainX.reshape(trainX.shape[0], 784)
  50. testX = testX.reshape(testX.shape[0], 784)
  51. # int to float
  52. trainX = trainX.astype('float32')
  53. testX = testX.astype('float32')
  54. trainX /= 255.0
  55. testX /= 255.0
  56. # one-hot encode
  57. trainY = keras.utils.to_categorical(trainY, 10)
  58. testY = keras.utils.to_categorical(testY, 10)
  59. input1 = Input(shape=(784,), name="input1")
  60. input2 = Input(shape=(10,), name="input2")
  61. x = Dense(1, activation='relu')(input1)
  62. output1 = Dense(10, activation='softmax', name="output1")(x)
  63. y = keras.layers.concatenate([x, input2])
  64. output2 = Dense(10, activation='softmax', name="output2")(y)
  65. model = Model(inputs=[input1, input2], outputs=[output1, output2])
  66. loss = {'output1': 'binary_crossentropy', 'output2': 'binary_crossentropy'}
  67. model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD(), loss_weights=[1, 0.1],
  68. metrics=['accuracy'])
  69. # 用字典可避免输入顺序不一致, 当然也可以用list [trainX, trainY], [trainY, trainY]
  70. model.fit(
  71. {'input1': trainX, 'input2': trainY},
  72. {'output1': trainY, 'output2': trainY},
  73. batch_size=128,
  74. epochs=20,
  75. validation_data=(
  76. [testX, testY], [testY, testY]
  77. )
  78. )
  79. # self_defined_fc()
  80. multi_io_structure()