<jupyter_start><jupyter_text>Tesorflow M1 MacBook お試し<jupyter_code>import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>y = 2 * x + 1 の簡単な回帰でfittingをデモ<jupyter_code>n_sample = 100
dataset_x = np.random.rand(n_sample)
dataset_y = 2 * dataset_x + 1 + np.random.normal(0, 0.05, n_sample) # ちょこっとノイズ加える
# plot
plt.scatter(dataset_x, dataset_y)<jupyter_output><empty_output><jupyter_text>自動微分を使って重みパラメータをfitting<jupyter_code>w = tf.Variable(np.zeros([1,1])) # fottingしたい変数
b = tf.Variable(np.zeros([1,1])) # あらかじめ正しい値を入れておく
x = tf.constant(dataset_x)
loss_log = [] # lossの推移記録用
n_step = 100 # 学習回数
for i in range(n_step):
    with tf.GradientTape() as t:
        t.watch(x) # tf.constantはwatachが必要らしい
        y = w * x + b # object function
        loss = tf.reduce_sum(tf.multiply(y - dataset_y, y - dataset_y)) # loss function
    # 元の入力テンソル w, b に対する loss の微分
    dloss_dparams = t.gradient(loss, [w, b]) # 自動微分
    w = tf.Variable(tf.subtract(w, dloss_dparams[0] * 0.001)) # 値の更新
    b = tf.Variable(tf.subtract(b, dloss_dparams[1] * 0.001)) # 値の更新
    loss_log.append(loss)
    #print(f'step {i + 1}: w = {w.numpy()}, b = {b.numpy()}, loss = {loss}')
# loss の推移を確認
print(f'w = {w.numpy()}, b = {b.numpy()}, loss = {loss}')
plt.plot([i for i in range(n_step)], loss_log)
plt.xlabel('step ')
plt.ylabel('loss')
plt.show()<jupyter_output>w = [[1.75216404]], b = [[1.14888086]], loss = 0.8518444412697204<jupyter_text>Optimizerを使用したパラメータの自動更新`tf.keras.optimizers`から使用できる。今回はSGDを使用<jupyter_code>w = tf.Variable(np.zeros([1,1])) # fottingしたい変数
b = tf.Variable(np.zeros([1,1])) # あらかじめ正しい値を入れておく
# optimizerの定義
opt = tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0)
x = tf.constant(dataset_x)
loss_log = [] # lossの推移記録用
n_step = 100 # 学習回数
for i in range(n_step):
    with tf.GradientTape() as t:
        t.watch(x) # tf.constantはwatachが必要らしい
        y = w * x + b # object function
        loss = tf.reduce_sum(tf.multiply(y - dataset_y, y - dataset_y)) # loss function
    # 元の入力テンソル w, b に対する loss の微分
    dloss_dparams = t.gradient(loss, [w, b]) # 自動微分
    # 微分結果を元に重み修正
    opt.apply_gradients(zip(dloss_dparams, [w, b]))
    loss_log.append(loss)
    
    #print(f'step {i + 1}: w = {w.numpy()}, b = {b.numpy()}, loss = {loss}')
# loss の推移を確認
print(f'w = {w.numpy()}, b = {b.numpy()}, loss = {loss}')
plt.plot([i for i in range(n_step)], loss_log)
plt.xlabel('step ')
plt.ylabel('loss')
plt.show()<jupyter_output>w = [[1.75216406]], b = [[1.14888085]], loss = 0.8518443511729884
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.044313725490196076, 'mean_word_length': 4.381856540084388, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '599504', 'license': 'MIT', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1237, 'n_words': 207, 'path': 'sample.ipynb', 'repo_name': 'yushikmr/m1mac_tensorflow', 'size': '2550'}