keras API的使用,神經網絡層,優化器,損失函數,查看模型層數,compile和fit訓練


layers介紹

 

 

Flatten和Dense介紹

 

 

優化器

 

 

 

損失函數

 

 

compile用法

 

 

 

第二個是onehot編碼

 

 

模型訓練 model.fit

 

 

 兩種創建模型的方法

from tensorflow.python.keras.preprocessing.image import load_img,img_to_array
from tensorflow.python.keras.models import Sequential,Model
from tensorflow.python.keras.layers import Dense,Flatten,Input
import tensorflow as tf
from tensorflow.python.keras.losses import sparse_categorical_crossentropy


def main():

    #通過Sequential創建網絡
    model = Sequential(
        [
            Flatten(input_shape=(28,28)),
            Dense(64,activation=tf.nn.relu),
            Dense(128,activation=tf.nn.relu),
            Dense(10,activation=tf.nn.softmax)
        ]
    )
    print(model)

   #通過Model創建模型
    data = Input(shape=(784,))
    out = Dense(64)(data)
    model_sec = Model(inputs=data,outputs=out)
    print(model_sec)
    print(model.layers,model_sec.layers)
    print(model.input,model.output)
    print(model.summary())
    print(model_sec.summary())

if __name__ == '__main__':
    main()

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM