Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีรวม Word Code Points ของ Ragged Tensor กลับเป็นประโยคด้วย TensorFlow

    บทนำในการประมวลผลภาษาธรรมชาติ (NLP) การ Segmentation (การแบ่งคำ) เป็นขั้นตอนสำคัญ โดยเฉพาะภาษาที่ไม่มี space คั่นคำ เช่น ไทย จีน ญี่ปุ่น หรือภาษาที่มีคำประสมยาว เช่น เยอรมันTensorFlow มีเครื่องมือ Unicode และ RaggedTensor สำหรับจัดการข้อความระดับ code point ได้อย่างมีประสิทธิภาพโค้ดตัวอย่างprint(Seg

  2. วิธีดาวน์โหลดและเตรียมข้อมูล CIFAR-10 ด้วย TensorFlow และ Python

    บทนำCIFAR-10 เป็น dataset ยอดนิยมสำหรับ image classification ประกอบด้วยรูปภาพสี 60,000 รูป ขนาด 32×32 พิกเซล แบ่ง 10 คลาส คลาสละ 6,000 รูปTensorFlow/Keras มีฟังก์ชัน datasets.cifar10.load_data() สำหรับดาวน์โหลดและแยก train/test อัตโนมัติโค้ดตัวอย่างimport tensorflow as tf from tensorflow.keras impor

  3. วิธีตรวจสอบข้อมูล CIFAR ด้วย TensorFlow และ Python

    บทนำหลังจากโหลดชุดข้อมูล CIFAR-10 แล้ว ขั้นตอนสำคัญคือการ ตรวจสอบข้อมูล (verify) โดยการพล็อตรูปภาพตัวอย่างเพื่อดูว่าโหลดถูกต้องและไลเบลตรงกันCIFAR labels เก็บเป็นอาร์เรย์ (array) จึงต้องใช้ดัชนีเพิ่มเติมเพื่อดึงชื่อคลาสโค้ดตัวอย่างprint(Verifying the data) plt.figure(figsize=(10,10)) print(Plot the

  4. วิธีสร้าง Convolutional Base ด้วย TensorFlow และ Python

    บทนำเครือข่ายประสาทเทียมแบบ Convolutional Neural Network (CNN) ประกอบด้วยเลเยอร์หลัก 3 ประเภท ได้แก่ Convolutional layers, Pooling layers และ Dense layers ซึ่งเหมาะสำหรับงานจดจำรูปภาพ (image recognition) เป็นพิเศษบทความนี้จะสาธิตวิธีสร้าง convolutional base (ส่วนสกัดลักษณะเฉพาะ) ด้วย Keras Sequentia

  5. TensorFlow: เพิ่ม Dense Layer บนโมเดล CNN ด้วย Python

    เพิ่ม Fully Connected (Dense) layers บน Convolutional base เพื่อการจำแนกประเภท (Classification) โค้ดตัวอย่าง print(เพิ่ม Dense layer บนสุด...) model.add(layers.Flatten()) model.add(layers.Dense(64, activation=relu)) model.add(layers.Dense(10)) print(สถาปัตยกรรมโมเดลสมบูรณ์:) model.summary() ผลลัพธ

  6. TensorFlow: คอมไพล์และฝึกโมเดล CNN

    คอมไพล์และฝึกโมเดล CNN (Convolutional Neural Network) สำหรับจำแนกภาพ โค้ดตัวอย่าง print(คอมไพล์โมเดล...) model.compile( optimizer=adam, loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[accuracy] ) print(ฝึกโมเดลให้เข้ากับข้อมูล...) history = model.fit(

  7. TensorFlow: ประเมินผลโมเดล CNN ด้วย Python

    ประเมินประสิทธิภาพโมเดล CNN ที่ฝึกแล้ว ด้วย model.evaluate() และวาดกราฟ accuracy โค้ดตัวอย่าง print(วาดกราฟ Accuracy ต่อ Epoch...) plt.plot(history.history[accuracy], label=accuracy) plt.plot(history.history[val_accuracy], label=val_accuracy) plt.xlabel(Epoch) plt.ylabel(Accuracy) plt.ylim([0.5, 1])

  8. TensorFlow: ดาวน์โหลด Flower Dataset ด้วย Keras Sequential API

    ดาวน์โหลด flower dataset จาก Google Cloud Storage ด้วย tf.keras.utils.get_file โค้ดตัวอย่าง import matplotlib.pyplot as plt import numpy as np import os import PIL import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.models import Sequential

  9. TensorFlow: สำรวจ Flower Dataset ด้วย Keras Sequential API และ PIL

    สำรวจข้อมูล flower dataset: นับจำนวนภาพ แสดงภาพตัวอย่างจากแต่ละคลาส โค้ดตัวอย่าง image_count = len(list(data_dir.glob(*/*.jpg))) print(จำนวนภาพใน dataset:, image_count) print(ตัวอย่างภาพ:) print(ROSES) roses = list(data_dir.glob(roses/*)) PIL.Image.open(str(roses[1])) print(TULIPS) tulips = list(

  10. TensorFlow: แบ่ง Flower Dataset เป็น Training และ Validation Set

    แบ่งข้อมูลภาพเป็น training set และ validation set ด้วย image_dataset_from_directory โค้ดตัวอย่าง batch_size = 32 img_height = 180 img_width = 180 print(แบ่งข้อมูลเป็น Training และ Validation set...) train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=

  11. TensorFlow: เตรียมข้อมูล Flower Dataset ด้วย Keras Preprocessing

    ใช้ keras.preprocessing.image_dataset_from_directory โหลดและแยก validation set จากโฟลเดอร์ภาพ โค้ดตัวอย่าง print(เตรียมข้อมูลด้วย keras.preprocessing...) val_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset=validation, seed=123, ima

  12. TensorFlow: แสดงภาพข้อมูล (Visualize) ด้วย Matplotlib

    แสดงภาพจาก flower dataset ที่โหลดผ่าน image_dataset_from_directory ด้วย matplotlib โค้ดตัวอย่าง print(แสดงภาพข้อมูล...) import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) for images, labels in train_ds.take(1): for i in range(6): ax = plt.subplot(3, 3, i + 1) plt.imsho

  13. TensorFlow: กำหนดค่าความสามารถ Dataset (Performance) ด้วย cache, shuffle, prefetch

    เพิ่มประสิทธิภาพการโหลดข้อมูลด้วย cache, shuffle, และ prefetch โค้ดตัวอย่าง print(กำหนดค่า Dataset สำหรับประสิทธิภาพ...) AUTOTUNE = tf.data.AUTOTUNE train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE) val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE) ผลลัพธ์ กำหนดค่า Data

  14. TensorFlow: มาตรฐาน hóaข้อมูล (Standardize) ด้วย Rescaling Layer

    ข้อมูลภาพ (RGB) มีค่า 0-255 ควรปรับให้อยู่ในช่วง 0-1 ก่อนฝึกโมเดล (Standardization) โดยใช้ Rescaling layer โค้ดตัวอย่าง print(สร้าง Normalization layer...) normalization_layer = layers.experimental.preprocessing.Rescaling(1./255) print(นำไปใช้กับ dataset ด้วย map function...) normalized_ds = train_

  15. TensorFlow: สร้าง Sequential Model ด้วย Keras API

    สร้างโมเดล Sequential (ลำดับชั้น) ด้วย Keras Sequential API ใช้สำหรับโมเดลที่เป็น stack ของ layers แบบตรงไปตรงมา โค้ดตัวอย่าง print(กำลังสร้าง Sequential model...) num_classes = 5 model = Sequential([ layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),

  16. TensorFlow: การคอมไพล์โมเดล (Compile) ด้วย Python

    โมเดลใน TensorFlow ต้องคอมไพล์ก่อนฝึก โดยระบุ optimizer, loss function, และ metrics โค้ดตัวอย่าง print(กำลังคอมไพล์โมเดล...) model.compile( optimizer=adam, loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[accuracy] ) print(สถาปัตยกรรมโมเดล:) model.summary()

  17. TensorFlow: การฝึกโมเดล (Training) ด้วย Python

    การฝึกโมเดลใน TensorFlow ใช้เมธอด fit() โดยระบุข้อมูลฝึก (training data), ข้อมูลตรวจสอบ (validation data), และจำนวนรอบ (epochs) โค้ดตัวอย่าง print(กำลังฝึกโมเดล...) epochs = 12 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs ) ผลลัพธ์ตัวอย่าง กำลังฝึกโมเดล Epoch 1/1

  18. เขียนโค้ด Python สุ่มเลือก 1 แถวจาก DataFrame

    สมมติว่าคุณมี DataFrame และต้องการสุ่มเลือก 1 แถว วิธีแก้ปัญหา สร้าง DataFrame หาจำนวนแถวด้วย df.shape[0] สุ่มเลข index ด้วย random.randrange(rows) ดึงแถวด้วย df.iloc[random_row, :] ตัวอย่างโค้ด import pandas as pd import random as r data = {Id: [1, 2, 3, 4, 5], Name: [Adam, Michael, Davi

  19. เขียนโปรแกรม Python กรองชื่อเมืองและจังหวัดที่ขึ้นต้นด้วย 'ก' บันทึกเป็น CSV

    สมมติว่าคุณมี DataFrame ที่มีคอลัมน์ City และ State ต้องการกรองเฉพาะแถวที่ทั้งสองคอลัมน์ขึ้นต้นด้วย ก (K) แล้วบันทึกเป็นไฟล์ CSV วิธีแก้ปัญหา สร้าง DataFrame กรองด้วย df[City].str.startswith(K) & df[State].str.startswith(K) บันทึกเป็น CSV ด้วย df.to_csv(test.csv) ตัวอย่างโค้ด import pandas a

  20. เขียนโปรแกรม Python นับปีอธิกสุรทิน (Leap Years) ใน DataFrame

    สมมติว่าคุณมี DataFrame ที่มีคอลัมน์ year และ days และต้องการนับจำนวนปีอธิกสุรทิน วิธีแก้ปัญหา 1. สร้าง DataFrame 2. กรองข้อมูลที่เป็นปีอธิกสุรทิน: year % 4 == 0 และ days == 366 3. นับจำนวนแถวที่กรองได้ด้วย len() ตัวอย่างโค้ด import pandas as pd data = {year: [2002, 2004, 2012, 2018, 2020],

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:230/451  20-คอมพิวเตอร์/Page Goto:1 224 225 226 227 228 229 230 231 232 233 234 235 236