差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
ディープラーニング入門_2025 [2025/03/18 08:28] – [練習問題 2-3] suikou | ディープラーニング入門_2025 [2025/03/24 07:23] (現在) – [練習問題3-1] suikou | ||
---|---|---|---|
行 125: | 行 125: | ||
Google Colab [[https:// | Google Colab [[https:// | ||
+ | |||
### 関数 | ### 関数 | ||
行 184: | 行 185: | ||
### 練習問題 2-3 | ### 練習問題 2-3 | ||
- | 1.下記のファイルは2024年度の学生実験で得られたナノポアのシーケンスデータ(FASTQファイル)です。これをGoogle Colabにアップロードし、ファイルを読み取り、リード数を調べてみる。(リード数=行数/ | + | 1.下記のファイルは2024年度の学生実験で得られたナノポアのシーケンスデータ(FASTQファイル)です。これをGoogle Colabにアップロードし、ファイルを読み取り、リード数を調べてみる。(リード数=行数/ |
[[https:// | [[https:// | ||
行 195: | 行 196: | ||
クラス、インスタンス、モジュール、Colabのランタイム変更、手書き数字の認識 | クラス、インスタンス、モジュール、Colabのランタイム変更、手書き数字の認識 | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ### コメント | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ### 関数のデフォルト値 | ||
+ | |||
+ | [[https:// | ||
### クラス、インスタンス | ### クラス、インスタンス | ||
- | [[https://www.tohoho-web.com/python/class.html]] | + | [[https://utokyo-ipp.github.io/6/6-3.html]] |
+ | |||
+ | ### 練習問題3-1 | ||
+ | |||
+ | ``` | ||
+ | A) `' | ||
+ | B)「初期化時に指定された文字列+何回呼び出されたか」を文字列で返す aisatsu メソッドを持つ新しいクラス SayCount を定義してください。ヒント:数字から文字列への変換はstr(数字)で行います。 | ||
+ | ``` | ||
### モジュール | ### モジュール | ||
行 204: | 行 222: | ||
[[https:// | [[https:// | ||
+ | ### 練習問題3-2 | ||
+ | |||
+ | 先ほどのSayCountクラスをsay.pyファイルとして保存して、Google Colabにアップロードし、自作sayモジュールを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を利用して手書き文字を認識するプログラムを作る。いくつか手書き文字を作成し、実際の正答率を調べてみる。 | ||