0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

子供向けのAI学習教材(日記)

Posted at

使っている数学知識(やさしく説明)

数学のジャンル 使われている考え方 どこで出てくる?
数と式 数字の範囲、分数、割り算、0〜1への変換 白黒の画像を「0〜1の数字」に変えるところ
図形と空間 28×28のマス目、位置、面積の感覚 画像がマス目でできている、平面を扱う
関数と変化 入力→出力の変化(関数の考え) ニューラルネットワークの「層」のしくみ
確率 「これは5っぽい確率が90%!」 最後のsoftmaxという処理で出てくる
統計 正解と不正解、正答率(accuracy) モデルの評価(何%あたったか)
ベクトル・行列(高校以降) 数をならべて、まとめて処理する Flatten層やDense層の中身(自動処理)

もう少し大人向けに

数学の概念 プログラム内での役割
正規化(0〜1のスケーリング) x_train = x_train / 255.0 画像の白黒を0〜1に直す(分数の理解)
ベクトル変換(Flatten) 28×28の画像を784個の1列に変える(次元の変換)
重み付き和と活性化関数 ニューラルネットワークの中身、( y = f(w_1x_1 + w_2x_2 + ... + b) )
分類(10種類に分ける) 出力が10個 → softmaxで確率に変換
誤差と最適化(損失関数) 正解とのズレ(cross entropy)を最小にするよう重みを学習

数学的キーワードまとめ

キーワード 学年目安 概要
正規化(スケーリング) 小5〜中学 255を1にする割り算、単位変換
座標と配列(行列) 中1〜高1 マス目・位置・リスト構造
関数(入力→出力) 中学〜 ( f(x) ) という考え方(活性化関数)
グラフと傾き 中2〜 学習結果をグラフで見る/変化の度合い
確率分布 中3〜高1 softmaxの出力 → 合計1になる確率の並び
誤差と最小化 高校〜大学 どこが間違ってる?を数で表して学習する

# プログラム名: ai_digit_recognition_easy.py
# 目的: AIが数字の絵を見て「これは〇!」ってあてる体験ができる

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.utils import to_categorical

# ① 手書き数字のデータをよみこむ / Load MNIST dataset
# x_train, y_train: AIが勉強する用の画像と答え
# x_test, y_test: テスト用(AIが当ててみる)
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# ② 画像の色を0〜1にする(AIが読みやすいように) / Normalize
x_train = x_train / 255.0
x_test = x_test / 255.0

# ③ AIの「数字あてクイズモデル」を作る / Build a simple neural network
model = Sequential([
    Flatten(input_shape=(28, 28)),    # 画像をまっすぐ1列にする(28×28 → 784)
    Dense(128, activation='relu'),    # 「かしこい考える場所」が128個
    Dense(10, activation='softmax')   # 出力は「0〜9の中からどれ?」を確率で出す
])
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# ④ 勉強スタート!(3回くりかえして覚える)/ Train the model
model.fit(x_train, to_categorical(y_train), epochs=3, batch_size=128)

# ⑤ AIにクイズを出す:1枚ランダムに選んでみせる / Pick one image randomly
index = np.random.randint(0, len(x_test))
img = x_test[index]

# ⑥ 画像を表示して、「これはなんの数字?」とたずねる / Show the image
plt.imshow(img, cmap='gray')
plt.title("What number is this?")
plt.axis('off')
plt.show()

# ⑦ AIに予想させよう!/ Let AI guess
prediction = model.predict(np.array([img]))
predicted_number = np.argmax(prediction)

# ⑧ 結果を表示!/ Show prediction result
print(f"🤖 AIのこたえ: {predicted_number}")
print(f"✅ ほんとうのこたえ: {y_test[index]}")

結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?