def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) ## ADDED THIS ## model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) ## ADDED THIS ## model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1000, activation='softmax')) if weights_path: model.load_weights(weights_path) return model
from keras.datasets import imdb # 确定一些超参数 max_features = 20000 # 使用最多的单词数 max_len = 80 # 循环截断的长度 batch_size = 32 # 加载、整理数据 (trainX,trainY),(testX,testY) = imdb.load_data(num_word = max_features) trainX = sequence.pad_sequences(trainX,maxlen = maxlen) testX = sequence.pad_sequences(testX,maxlen = maxlen) # 模型构建 model = Sequential() model.add( Embedding(max_features,128) ) model.add( LSTM(128,dropout=0.2,recurrent_dropout=0.2) ) model.add( Dense(1,activation='sigmoid') ) # 损失函数、优化函数的配置 model.compile(loss = 'binary_crossentropy', optimizer = 'adam',metrics=['accuracy']) model.fit(trainX,trainY,batch_size = batch_size,epoch=15,validation_data = (testX,testY)) score = model.evaluate(testX,testY) print('test loss:',score[0])
for i in range(Y.shape[0]): for j in range(a.shape[0]): if Y[i]==a[j]: Y[i]=j # convert class labels to on-hot encoding Y = np_utils.to_categorical(Y,num_classes) #Y.shape Y[48] #suffle dataset x,y = shuffle(X,Y, random_state=2) X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2) # Defining the model input_shape=X[0].shape #img_data.shape print(input_shape) model = Sequential() model.add(Conv2D(96,(11,11),subsample=(4, 4),input_shape=input_shape))#model.output_shape model.add(MaxPooling2D(pool_size=(2, 2)))#model.output_shape model.add(ZeroPadding2D(padding=(2, 2)))#model.output_shape model.add(Conv2D(256, (5, 5)))#model.output_shape model.add(MaxPooling2D(pool_size=(2, 2)))#model.output_shape model.add(ZeroPadding2D(padding=(1, 1)))#model.output_shape model.add(Conv2D(384, (3,3)))#model.output_shape model.add(ZeroPadding2D(padding=(1, 1))) model.add(Conv2D(384, (3,3)))#model.output_shape model.add(ZeroPadding2D(padding=(1, 1)))#model.output_shape model.add(Conv2D(256, (3,3)))#model.output_shape model.add(MaxPooling2D(pool_size=(2, 2)))#model.output_shape model.add(Flatten())#model.output_shape model.add(Dense(2048))#model.output_shape model.add(Dense(2048,name="dense_layer"))#model.output_shape model.add(Dense(num_classes))#model.output_shape
def build_classifier(): classifier=Sequential() classifier.add(Convolution2D(32,3,3,input_shape=(128,128,3),activation='relu')) classifier.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')) classifier.add(Convolution2D(64,3,3,activation='relu')) classifier.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same')) classifier.add(Flatten()) classifier.add(Dense(output_dim=64,activation='relu')) classifier.add(Dropout(p=0.5)) classifier.add(Dense(output_dim=1,activation='sigmoid')) classifier.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy']) return classifier
rom keras.models import Sequential from keras.layers import Convolution2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense # Initialising the CNN classifier = Sequential() # Step 1 - Convolution classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu')) # Step 2 - Pooling classifier.add(MaxPooling2D(pool_size = (2, 2))) # Adding a second convolutional layer classifier.add(Convolution2D(32, 3, 3, activation = 'relu')) classifier.add(MaxPooling2D(pool_size = (2, 2))) # Step 3 - Flattening classifier.add(Flatten()) # Step 4 - Full connection classifier.add(Dense(output_dim = 128, activation = 'relu')) classifier.add(Dense(output_dim = 1, activation = 'sigmoid')) # Compiling the CNN classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) # Part 2 - Fitting the CNN to the images
train_x = list(training[:,0]) train_y = list(training[:,1]) print("Training data created") """# 4. Build machine learning model After creating training data, build a deep neaural network that has 3 layers. The following code uses Keras' sequential API. The model is trained for 200 epochs, achieving 100% accuracy on the model. """ # Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd output layer contains number of neurons # equal to number of intents to predict output intent with softmax model = Sequential() model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(len(train_y[0]), activation='softmax')) # Compile model. Stochastic gradient descent with Nesterov accelerated gradient gives good results for this model sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) #fitting and saving the model hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1) model.save('chatbot_model.h5', hist) print("model created")
import keras import keras.models import Sequential import keras.layers Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.callbacks import TensorBoard import os import numpy as np import random model = Sequential() model.add(Conv2D(32, (3, 3), padding = 'same', input_shape(176, 200, 3), activation = 'relu')) model.add(Conv2D(32, (3, 3), activation = 'relu')) model.add(MaxPooling2D(pool_size = (2, 2))) model.add(Dropout(0.2)) model.add(Conv2D(64, (3, 3), padding = 'same', activation = 'relu')) model.add(Conv2D(64, (3, 3), activation = 'relu')) model.add(MaxPooling2D(pool_size = (2, 2))) model.add(Dropout(0.2)) model.add(Conv2D(128, (3, 3), padding = 'same', activation = 'relu')) model.add(Conv2D(128, (3, 3), activation = 'relu')) model.add(MaxPooling2D(pool_size = (2, 2))) model.add(Dropout(0.2)) # fully-connected dense layer model.add(Flatten()) model.add(Dense(512, activation = 'relu'))