Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

API การทำงานสามารถใช้เพื่อทำงานกับการเชื่อมต่อที่เหลือใน Python ได้อย่างไร


Keras มีอยู่ในแพ็คเกจ Tensorflow สามารถเข้าถึงได้โดยใช้รหัสบรรทัดด้านล่าง

นำเข้า tensorflow จาก tensorflow นำเข้า keras

Keras functional API ช่วยสร้างโมเดลที่มีความยืดหยุ่นมากกว่าเมื่อเปรียบเทียบกับโมเดลที่สร้างโดยใช้ Sequential API API ที่ใช้งานได้สามารถทำงานกับโมเดลที่มีโทโพโลยีที่ไม่ใช่เชิงเส้น สามารถแชร์เลเยอร์และทำงานกับอินพุตและเอาต์พุตได้หลายรายการ โมเดลการเรียนรู้เชิงลึกมักจะเป็นกราฟ acyclic แบบกำกับทิศทาง (DAG) ที่มีหลายเลเยอร์ API การทำงานช่วยสร้างกราฟของเลเยอร์

เรากำลังใช้ Google Colaboratory เพื่อเรียกใช้โค้ดด้านล่าง Google Colab หรือ Colaboratory ช่วยเรียกใช้โค้ด Python บนเบราว์เซอร์และไม่ต้องมีการกำหนดค่าใดๆ และเข้าถึง GPU ได้ฟรี (หน่วยประมวลผลกราฟิก) Colaboratory ถูกสร้างขึ้นบน Jupyter Notebook ต่อไปนี้เป็นข้อมูลโค้ด

ตัวอย่าง

print("Toy ResNet model for CIFAR10")print("Layers created for model") อินพุต =keras.Input(shape=(32, 32, 3), name="img")x =layer.Conv2D( 32, 3, enable="relu")(อินพุต)x =layer.Conv2D(64, 3, enable="relu")(x)block_1_output =layer.MaxPooling2D(3)(x)x =layer.Conv2D(64) , 3, enable="relu", padding="same")(block_1_output)x =layer.Conv2D(64, 3, enable="relu", padding="same")(x)block_2_output =layer.add([ x, block_1_output])x =layer.Conv2D(64, 3, enable="relu", padding="same")(block_2_output)x =layer.Conv2D(64, 3, enable="relu", padding="same ")(x)block_3_output =layer.add([x, block_2_output])x =layer.Conv2D(64, 3, activation="relu")(block_3_output)x =layer.GlobalAveragePooling2D()(x)x =เลเยอร์ หนาแน่น (256, การเปิดใช้งาน ="relu") (x) x =layer.Dropout(0.5)(x)outputs =layer.Dense(10)(x)model =keras.Model(inputs, outputs, name="toy_resnet" )print("ข้อมูลเพิ่มเติมเกี่ยวกับโมเดล") model.summary()

เครดิตโค้ด – https://www.tensorflow.org/guide/keras/functional

ผลลัพธ์

Toy ResNet model สำหรับ CIFAR10Layers สร้างขึ้นสำหรับ model ข้อมูลเพิ่มเติมเกี่ยวกับ model Model:"toy_resnet"__________________________________________________________________________________________________ เลเยอร์ (ประเภท) Output Shape Param # Connected to==================================================================================================img (InputLayer) [(ไม่มี, 32, 32, 3)] 0__________________________________________________________________________________________________conv2d_32 (Conv2D) (ไม่มี, 30 , 30, 32) 896 img[0][0]__________________________________________________________________________________conv2d_33 (Conv2D) (ไม่มี, 28, 28, 64) 18496 conv2d_32[0][0]__________________________________________________________________________________max_pooling2d_8 (MaxPooling2D) (ไม่มี, 9, 9, 64) 0 conv2d_33[ 0][0]_____________________________________________________________________________________ _____________________________conv2d_34 (Conv2D) (ไม่มี, 9, 9, 64) 36928 max_pooling2d_8[0][0]__________________________________________________________________________________conv2d_35 (Conv2D) (ไม่มี, 9, 9, 64) 36928 conv2d_34[0][0]__________________________________________________________________________________________________addone_12 (เพิ่ม) (N) , 9, 64) 0 conv2d_35[0][0] max_pooling2d_8[0][0]__________________________________________________________________________________________________conv2d_36 (Conv2D) (ไม่มี, 9, 9, 64) 36928 add_12[0][0]__________________________________________________________________________________________________conv2d_37 (Conv2D) (ไม่มี, 9, 9, 64) 36928 conv2d_36[0][0]__________________________________________________________________________________________________add_13 (เพิ่ม) (ไม่มี, 9, 9, 64) 0 conv2d_37[0][0] add_12[0][0]__________________________________________________________________________________ conv2d_38 (Conv2D) (ไม่มี, 7, 7, 64) 36928 add_13[0][0]__________________________________________________________________________________global_average_pooling2d_1 (Glo (ไม่มี, 64) 0 conv2d_38[0 ][0]________________________________________________________________________________________________dense_40 (หนาแน่น) (ไม่มี, 256) 16640 global_average_pooling2d_1[0][0]___________________________________________________________________________________________________dropout_2 (Dropout) (ไม่มี, 256) 0 Density_40[0][0]__________________________________________________________________________________________________dense_41 (หนาแน่น) (ไม่มี, 10) 2570 dropout 0][0]==================================================================================================================================================================================================================================================================================================================================================================================================

คำอธิบาย

  • โมเดลนี้มีอินพุตและเอาต์พุตหลายรายการ

  • API การทำงานช่วยให้ทำงานกับโทโพโลยีการเชื่อมต่อที่ไม่ใช่เชิงเส้นได้ง่าย

  • โมเดลที่มีเลเยอร์นี้ไม่ได้เชื่อมต่อตามลำดับ ดังนั้น API 'ลำดับ' จึงไม่สามารถทำงานได้

  • นี่คือจุดที่การเชื่อมต่อที่เหลือเข้ามาในรูปภาพ

  • ตัวอย่างโมเดล ResNet โดยใช้ CIFAR10 สร้างขึ้นเพื่อสาธิตแบบเดียวกัน