差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
| ディープラーニング入門_2025 [2025/03/19 18:30] – suikou | ディープラーニング入門_2025 [2025/05/28 03:40] (現在) – suikou | ||
|---|---|---|---|
| 行 192: | 行 192: | ||
| {{: | {{: | ||
| + | |||
| + | #### 解答例 | ||
| + | |||
| + | 1. | ||
| + | |||
| + | ``` | ||
| + | f = open(" | ||
| + | data = f.read() | ||
| + | mylist = data.split(" | ||
| + | cnt = 0 | ||
| + | for line in mylist: | ||
| + | cnt = cnt + 1 | ||
| + | print((cnt - 1)/4) # | ||
| + | ``` | ||
| + | |||
| + | 2. | ||
| + | |||
| + | ``` | ||
| + | f = open(" | ||
| + | data = f.read() | ||
| + | mylist = data.split(" | ||
| + | cnt = 0 | ||
| + | total = 0 | ||
| + | for line in mylist: | ||
| + | cnt = cnt + 1 | ||
| + | if cnt % 4 == 2: # | ||
| + | total = total + len(line) # | ||
| + | print((cnt - 1)/4) # | ||
| + | print(total/ | ||
| + | ``` | ||
| ## Day 3 (2025/ | ## Day 3 (2025/ | ||
| 行 197: | 行 227: | ||
| クラス、インスタンス、モジュール、Colabのランタイム変更、手書き数字の認識 | クラス、インスタンス、モジュール、Colabのランタイム変更、手書き数字の認識 | ||
| - | [[https:// | + | [[https:// |
| ### コメント | ### コメント | ||
| 行 206: | 行 236: | ||
| [[https:// | [[https:// | ||
| + | |||
| + | ### 値渡しと参照渡し | ||
| + | |||
| + | [[https:// | ||
| ### クラス、インスタンス | ### クラス、インスタンス | ||
| 行 213: | 行 247: | ||
| ### 練習問題3-1 | ### 練習問題3-1 | ||
| - | `' | + | ``` |
| + | A) `' | ||
| + | B) 初期化するときにHelloForEver(' | ||
| + | ``` | ||
| ### モジュール | ### モジュール | ||
| 行 219: | 行 256: | ||
| [[https:// | [[https:// | ||
| + | ### 練習問題3-2 | ||
| + | |||
| + | 先ほどのHelloForEverクラスをhello.pyファイルとして保存して、Google Colabにアップロードし、自作helloモジュールをimportで読み込んで、適当な引数で初期化して使ってみる。 | ||
| + | |||
| + | ### 手書き文字認識 | ||
| + | |||
| + | ``` | ||
| + | # prompt: 手書き文字を認識するプログラムを書いて | ||
| + | |||
| + | !pip install tensorflow | ||
| + | !pip install keras | ||
| + | !pip install pillow | ||
| + | |||
| + | import tensorflow as tf | ||
| + | from tensorflow import keras | ||
| + | from tensorflow.keras import layers | ||
| + | from PIL import Image | ||
| + | import numpy as np | ||
| + | |||
| + | # MNISTデータセットをロード | ||
| + | (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() | ||
| + | |||
| + | # データの前処理 | ||
| + | x_train = x_train.astype(' | ||
| + | x_test = x_test.astype(' | ||
| + | y_train = keras.utils.to_categorical(y_train, | ||
| + | y_test = keras.utils.to_categorical(y_test, | ||
| + | |||
| + | # モデルの構築 | ||
| + | model = keras.Sequential([ | ||
| + | layers.Flatten(input_shape=(28, | ||
| + | layers.Dense(128, | ||
| + | layers.Dense(10, | ||
| + | ]) | ||
| + | |||
| + | # モデルのコンパイル | ||
| + | model.compile(optimizer=' | ||
| + | loss=' | ||
| + | metrics=[' | ||
| + | |||
| + | # モデルの学習 | ||
| + | model.fit(x_train, | ||
| + | |||
| + | # モデルの評価 | ||
| + | test_loss, test_acc = model.evaluate(x_test, | ||
| + | print(' | ||
| + | |||
| + | # 手書き文字の認識 | ||
| + | def recognize_handwritten_digit(image_path): | ||
| + | img = Image.open(image_path).convert(' | ||
| + | img_array = np.array(img) | ||
| + | img_array = img_array.astype(' | ||
| + | img_array = np.expand_dims(img_array, | ||
| + | prediction = model.predict(img_array) | ||
| + | digit = np.argmax(prediction) | ||
| + | return digit | ||
| + | |||
| + | # 手書き文字画像のパスを指定して認識 | ||
| + | image_path = ' | ||
| + | recognized_digit = recognize_handwritten_digit(image_path) | ||
| + | print(' | ||
| + | ``` | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Mac用のペイントソフト: | ||
| + | |||
| + | {{2.png}} | ||
| + | |||
| + | 手書き文字は黒字に白文字で書くこと! | ||
| + | |||
| + | ### Colabのランタイム変更 | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | 参考:T4のGPUカードのお値段 | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ### 畳み込みニューラルネットワーク(CNN) | ||
| + | |||
| + | 動物の脳が光情報を処理するメカニズム [[https:// | ||
| + | |||
| + | CNNについて [[https:// | ||
| + | |||
| + | ### 最終課題 | ||
| + | |||
| + | CNNを利用して手書き文字を認識するプログラムを作る。いくつか手書き文字を作成し、実際の正答率を調べてみる。 | ||