欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Keras_tutorial-NeuralNetwork

程序员文章站 2022-03-14 21:21:16
import numpy as npfrom keras import layersfrom keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2Dfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2Df...
import numpy as np
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydotplus
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from kt_utils import *

import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

%matplotlib inline
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Normalize image vectors
X_train = X_train_orig/255.
X_test = X_test_orig/255.

# Reshape
Y_train = Y_train_orig.T
Y_test = Y_test_orig.T

print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

結果:
number of training examples = 600
number of test examples = 150
X_train shape: (600, 64, 64, 3)
Y_train shape: (600, 1)
X_test shape: (150, 64, 64, 3)
Y_test shape: (150, 1)
def HappyModel(input_shape):
    X_input = Input(input_shape)
    X =ZeroPadding2D((3,3))(X_input)
    X = Conv2D(32,(7,7), strides=(1, 1),name='conv0')(X)
    X = BatchNormalization(axis=3,name='bn0')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((2, 2), name='max_pool')(X)
    X = Flatten()(X)
    X = Dense(1, activation='sigmoid', name='fc')(X)
    model = Model(inputs = X_input, outputs = X, name='HappyModel')
    
    return model

model = HappyModel(X_train.shape[1:])
model.compile(optimizer = 'Adam', loss = "binary_crossentropy", metrics = ["accuracy"])
model.fit(X_train,Y_train,epochs=10,batch_size=32)
train_loss,train_accuracy = model.evaluate(X_train,Y_train)
test_loss,test_accuracy = model.evaluate(X_test,Y_test)
print('train_loss:',train_loss)
print('train_accuracy:',train_accuracy)
print('test_loss:',test_loss)
print('test_accuracy:',test_accuracy)

结果:
Epoch 1/10
600/600 [==============================] - 2s 4ms/step - loss: 2.1304 - acc: 0.5933
Epoch 2/10
600/600 [==============================] - 1s 1ms/step - loss: 0.3878 - acc: 0.8350
Epoch 3/10
600/600 [==============================] - 1s 1ms/step - loss: 0.1810 - acc: 0.9283
Epoch 4/10
600/600 [==============================] - 1s 1ms/step - loss: 0.1202 - acc: 0.9583
Epoch 5/10
600/600 [==============================] - 1s 1ms/step - loss: 0.1020 - acc: 0.9633
Epoch 6/10
600/600 [==============================] - 1s 1ms/step - loss: 0.0737 - acc: 0.9817
Epoch 7/10
600/600 [==============================] - 1s 1ms/step - loss: 0.0728 - acc: 0.9783
Epoch 8/10
600/600 [==============================] - 1s 1ms/step - loss: 0.0700 - acc: 0.9800
Epoch 9/10
600/600 [==============================] - 1s 1ms/step - loss: 0.0620 - acc: 0.9783
Epoch 10/10
600/600 [==============================] - 1s 1ms/step - loss: 0.0682 - acc: 0.9750
600/600 [==============================] - 1s 1ms/step
150/150 [==============================] - 0s 853us/step
train_loss: 0.06977755357821783
train_accuracy: 0.99
test_loss: 0.11974947273731232
test_accuracy: 0.9600000039736429

Keras练习,对,我就想展示下时间,就想展示下时间(以后一定要换1060)
Keras_tutorial-NeuralNetwork

本文地址:https://blog.csdn.net/zyoulanxin/article/details/110249767