Convolution -Maxpooling循環結束後
就要把向量拉直,放到全連階層
output 出來也不像是數字
只像是電視的雜訊
兩極化範例
Original : w1 = +3 , w2 = -1
After : w1 = +6 , w2 = -4
原圖
Deep Dream
原圖
Deep Style
第一點
第二點
MNIST CNN CODE1
# 設定所需 library import numpy as np from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.layers import Conv2D, MaxPooling2D, Flatten from keras.optimizers import SGD, Adam from keras.utils import np_utils from keras.datasets import mnist import matplotlib.pyplot as plt
MNIST CNN CODE2
# categorical_crossentropy def load_data(): (x_train, y_train), (x_test, y_test) = mnist.load_data() number = 10000 x_train = x_train[0:number] y_train = y_train[0:number] x_train = x_train.reshape(number, 28 , 28,1) x_test = x_test.reshape(number, 28 , 28,1) x_train = x_train.astype('float32') x_test = x_test.astype('float32') # convert class vectors to binary class matrices y_train = np_utils.to_categorical(y_train, 10) y_test = np_utils.to_categorical(y_test, 10) x_train = x_train x_test = x_test # x_test=np.random.normal(x_test) x_train = x_train/255 x_test = x_test/255 return (x_train, y_train), (x_test, y_test) def show_train_history(train_history, train, validation): plt.plot(train_history.history[train]) plt.plot(train_history.history[validation]) plt.title('Train History') plt.ylabel(train) plt.xlabel('Epoch') plt.legend(['train', 'validation'], loc='upper left') plt.show()
MNIST CNN CODE3
(x_train, y_train), (x_test, y_test) = load_data() # define network structure model = Sequential() model.add(Conv2D(25,(3,3), activation='relu', padding='same',input_shape=(28,28,1))) model.add(MaxPooling2D(pool_size = (2, 2),dim_ordering="th")) #model.add(Dropout(0.5)) model.add(Conv2D(50,(3,3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2,2),dim_ordering="th")) model.add(Flatten()) model.add(Dense(units=10, activation='softmax')) model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['acc']) train_history = model.fit(x_train, y_train, batch_size=100, epochs=20,validation_split=0.2) result = model.evaluate(x_train, y_train) print('Train Accuracy:', result[1]) result = model.evaluate(x_test, y_test) print('Test Accuracy:', result[1]) show_train_history(train_history, 'acc', 'val_acc') show_train_history(train_history, 'loss', 'val_loss')
MNIST CNN CODE4
from keras.models import load_model # creates a HDF5 file model.save('CNN-1.h5') if load_model('CNN-1.h5'): print('Yeee~')
Use your finger
Deep learning
beginner
python
keras
tutorial