7. Deep Learning From Scratch
生活随笔
收集整理的這篇文章主要介紹了
7. Deep Learning From Scratch
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Intro
這是深度學習第7課。
到目前為止,您構建的模型依賴于預先訓練的模型。 但它們不是許多用例的理想解決方案。 在本課程中,您將學習如何構建全新的模型。
Lesson
[1]
from IPython.display import YouTubeVideo YouTubeVideo('YbNE3zhtsoo', width=800, height=450)Sample Code
[2]
import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from tensorflow.python import keras from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropoutimg_rows, img_cols = 28, 28 num_classes = 10def data_prep(raw):out_y = keras.utils.to_categorical(raw.label, num_classes)num_images = raw.shape[0]x_as_array = raw.values[:,1:]x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)out_x = x_shaped_array / 255return out_x, out_ytrain_file = "../input/digit-recognizer/train.csv" raw_data = pd.read_csv(train_file)x, y = data_prep(raw_data)model = Sequential() model.add(Conv2D(20, kernel_size=(3, 3),activation='relu',input_shape=(img_rows, img_cols, 1))) model.add(Conv2D(20, kernel_size=(3, 3), activation='relu')) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dense(num_classes, activation='softmax'))model.compile(loss=keras.losses.categorical_crossentropy,optimizer='adam',metrics=['accuracy']) model.fit(x, y,batch_size=128,epochs=2,validation_split = 0.2) /opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_convertersTrain on 33600 samples, validate on 8400 samples Epoch 1/2 33600/33600 [==============================] - 56s 2ms/step - loss: 0.2454 - acc: 0.9292 - val_loss: 0.0823 - val_acc: 0.9749 Epoch 2/2 33600/33600 [==============================] - 58s 2ms/step - loss: 0.0650 - acc: 0.9810 - val_loss: 0.0590 - val_acc: 0.9820<tensorflow.python.keras._impl.keras.callbacks.History at 0x7fa843394978>?
Exercise: Modeling from Scratch
Introduction
您已經了解了如何從頭開始構建模型以識別手寫數字。 您現在將構建一個模型來識別不同類型的服裝。 為了制作快速訓練的模型,我們將使用非常小(低分辨率)的圖像。
例如,您的模型將采用這樣的圖像并將其識別為鞋子:
Data Preparation
代碼如下,您不需要去修改他,運行一下代碼。
【3】
import numpy as np from sklearn.model_selection import train_test_split from tensorflow.python import kerasimg_rows, img_cols = 28, 28 num_classes = 10def prep_data(raw, train_size, val_size):y = raw[:, 0]out_y = keras.utils.to_categorical(y, num_classes)x = raw[:,1:]num_images = raw.shape[0]out_x = x.reshape(num_images, img_rows, img_cols, 1)out_x = out_x / 255return out_x, out_yfashion_file = "../input/fashionmnist/fashion-mnist_train.csv" fashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',') x, y = prep_data(fashion_data, train_size=50000, val_size=5000)Specify Model
STEPS:
【4】
from tensorflow.python import keras from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, Conv2D# Your Code HereCompile Model
運行命令fashion_model.compile。指定以下參數:
【5】
# Your code to compile the model in this cellFit Model
運行命令fashion_model.fit。你將會用到的參數有:
當你運行此命令時,你會看到你的模型正在改善,您將看到驗證的準確性。
[6]
# Your code to fit the model hereKeep Going
繼續了解這里的strides和dropout.。
總結
以上是生活随笔為你收集整理的7. Deep Learning From Scratch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今晚油价下调 95号汽油重回10元内:下
- 下一篇: 曝小米12S Pro不止有纯白色机身:还