Chapter 10 :Introduction to Artificial Neural Networks with Keras

#Introduction to Artificial Neural Networks with Keras
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron
import tensorflow as tf
print(tf.__version__) #2.8.2
from tensorflow import keras
print(keras.__version__) #2.8.0
import pandas as pd
import matplotlib.pyplot as plt

#Perceptrons
iris = load_iris()
X = iris.data[:,(2,3)] #petal legth, petal width
y = (iris.target == 0).astype(np.int64) #iris setosa(category)

per_clf = Perceptron()
per_clf.fit(X,y)
y_pred = per_clf.predict([[2,0.5]])
print(y_pred)

#Buiding image classifier using the Sequential API
#load data
fashion_mnist = keras.datasets.fashion_mnist
(X_train_full,y_train_full),(X_test,y_test) = fashion_mnist.load_data()

print(X_train_full.shape) #(60000, 28, 28)
print(X_train_full.dtype) #uint8

#The dataset is already split into a training set and a test set
#Because train the neural network using Gradient Descent, must scale the input features
X_valid, X_train = X_train_full[:5000] / 255.0, X_train_full[5000:] / 255.0
y_valid, y_train = y_train_full[:5000] , y_train_full[5000:]
X_test = X_test / 255.0

class_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']

#First image in the training set represents a coat
print(class_names[y_train[0]])
#Coat

#Build Neual Network, A classification MLP with 2 hidden layer using Sequential Model
model = keras. models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
#input 28*28, Flatten means to convert each input image into a 1D array,this layer does not have any parameters
model.add(keras.layers.Dense(300,activation='relu')) #300 neurons
#each dense layer manges its own weight matrix
model.add(keras.layers.Dense(100,activation='relu'))
model.add(keras.layers.Dense(10,activation='softmax'))
#output layer for 10 neurons(one for per class) using softmax activation function

#Simple way using Sequential Model
model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(300,activation='relu'),
    keras.layers.Dense(100,activation='relu'),
    keras.layers.Dense(10,activation='softmax')
])

print(model.summary())
'''
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten_1 (Flatten)         (None, 784)               0         
                                                                 
 dense_3 (Dense)             (None, 300)               235500    
#28*28*300 + 300 bias = 235500                                                              
 dense_4 (Dense)             (None, 100)               30100     
                                                                 
 dense_5 (Dense)             (None, 10)                1010      
                                                                 
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________
None
'''
print(model.layers)
#[<keras.layers.core.flatten.Flatten object at 0x000001D4B6857160>,
# <keras.layers.core.dense.Dense object at 0x000001D4B68570D0>,
# <keras.layers.core.dense.Dense object at 0x000001D4B6856FE0>,
# <keras.layers.core.dense.Dense object at 0x000001D4B6855180>]
#each layer has their index

hidden1 = model.layers[1]
print(hidden1.name)
#print(model.get_layer('dense') is hidden1)
weights, biases = hidden1.get_weights()
print(weights.shape)
print(biases.shape)
'''

#Compiling the Model
#model.compile(loss='sparse_categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])

#Training and Evaluating the model
history = model.fit(X_train,y_train,epochs=30,
                    validation_data=(X_valid,y_valid))

#create pandas DataFrame to show the parameters
pd.DataFrame(history.history).plot(figsize=(8,5))
plt.grid(True)
plt.gca().set_ylim(0,1) #set the vertical range to [0,1]
plt.show()

model.evaluate(X_test,y_test)
#313/313 [==============================] - 1s 879us/step - loss: 2.3331 - accuracy: 0.1202
#difference with the book, the book has 1000 and acc is 88.51%

#Using model to Prediction,using predict()
X_new = X_test[:3]
y_proba = model.predict(X_new)
#print(y_proba.round(2))

#ErrorAttributeError: 'Sequential' object has no attribute 'predict_classes'. Did you mean: 'predict_step'?
#y_pred= model.predict_classes(X_new)
y_pred = model.predict(X_new)
y_pred =(model.predict(X_new) > 0.5).astype("int32")
'''
'''
astype()函数可用于转化dateframe某一列的数据类型,如下将dateframe某列的str类型转为int
astype()    对数据类型进行转换  
'''
#print(y_pred)
#[9 2 1]
'''


[[0.   0.   0.   0.   0.   0.   0.   0.01 0.   0.98]
 [0.   0.   0.99 0.   0.   0.   0.   0.   0.   0.  ]
 [0.   1.   0.   0.   0.   0.   0.   0.   0.   0.  ]]

#For each instance, the model estimates one probability for each class,from class 0-9
class_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']
[0.   0.   0.   0.   0.   0.   0.   0.01 0.   0.98]
For the first image, the probability for Ankle boot is 0.98,and that of Bag is 0.01[class=9 ]
 [0.   0.   0.99 0.   0.   0.   0.   0.   0.   0.  ]
For the second image, the probability for Pullover is 0.99,rest of them are 0[class=2]
 [0.   1.   0.   0.   0.   0.   0.   0.   0.   0.  ]
For the third image, the probability for Trousers are 1,100%, and rest of them are 0[class=1]


#The classifier actually calssified all three images correctly
y_new = y_test[:3]
print(y_new)
'''


#Building a Regression MLP using the Sequential API
#California housing problem
from sklearn.datasets import fetch_california_housing #get data
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

housing = fetch_california_housing()

X_train_full,X_test,y_train_full,y_test = train_test_split(
    housing.data,housing.target)

X_train,X_valid,y_train,y_valid = train_test_split(
    X_train_full,y_train_full)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)

#use a single hidden layer with fewer neurons to avoid overfitting
model = keras.models.Sequential([
    keras.layers.Dense(30,activation ='relu',input_shape=X_train.shape[1:]),
    keras.layers.Dense(1)
])

model.compile(loss='mean_squared_error',optimizer='sgd')
history = model.fit(X_train,y_train,epochs=20,
                    validation_data = (X_valid,y_valid))
mse_test = model.evaluate(X_test,y_test)
X_new = X_test[:3] #pretend these are new instance
y_pred = model.predict(X_new)
print(y_pred)

#using subclass API to build dynamic models
class WideAndDeepModel(keras.Model):
    def __int__(self,units=30,activation='relu',**kwargs):
        super().__init__(**kwargs) #handles standard args(eg,name)
        self.hidden1 = keras.layers.Dense(units,activation=activation)
        self.hidden2 = keras.layers.Dense(units,activation=activation)
        self.main_output = keras.layers.Dense(1)
        self.aux_output = keras.layers.Dense(1)

    def call(self,inputs):
        input_A,input_B = inputs
        hidden1 = self.hidden1(input_B)
        hidden2 = self.hidden2(hidden1)
        contact = keras.layers.concatenate([input_A,input_B])
        main_output = self.aux.output(contact)
        aux_output = self.aux_output(hidden2)
        return main_output,aux_output

model = WideAndDeepModel()

'''
#Saving and Restoring a Model
model = keras.models.Sequential([) 
model.compile([])
model.fit([])
model.save('my_keras_model.h5')
#load model
model = keras.models.load_model('my_keras_model.h5')
'''


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值