<jupyter_start><jupyter_text>Running example: Train a two-layer ReLU network on random data with L2 loss<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
N, D, H = 64, 1000, 100
x = tf.placeholder(tf.float32, shape=(N, D))
y = tf.placeholder(tf.float32, shape=(N, D))
w1 = tf.placeholder(tf.float32, shape=(D, H))
w2 = tf.placeholder(tf.float32, shape=(H, D))
h = tf.maximum(tf.matmul(x, w1), 0)
y_pred = tf.matmul(h, w2)
diff = y_pred - y
loss = tf.reduce_mean(tf.reduce_sum(diff ** 2, axis=1))

grad_w1, grad_w2 = tf.gradients(loss, [w1, w2])
with tf.Session() as sess:
    values = {x: np.random.randn(N, D),
             w1: np.random.randn(D, H),
             w2: np.random.randn(H, D),
             y: np.random.randn(N, D),}
    losses = []
    learning_rate = 1e-5
    for t in range(50):
        out = sess.run([loss, grad_w1, grad_w2], feed_dict=values)
        loss_val, grad_w1_val, grad_w2_val = out
        losses.append(loss_val)
        values[w1] -= learning_rate * grad_w1_val
        values[w2] -= learning_rate * grad_w2_val
    plt.plot(losses)
    plt.grid(True)
    plt.title('Losses')
    plt.xlabel('batch number')
    plt.ylabel('batch loss')
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14791502753737215, 'fraction_numerical': 0.03383162863886703, 'mean_word_length': 3.326530612244898, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '296893', 'license': 'Apache-2.0', 'n_tokens_mistral': 522, 'n_tokens_neox': 503, 'n_words': 115, 'path': 'cs231n/assignment2/test/tf-two-layer.ipynb', 'repo_name': 'Rockung/sma', 'size': '1271'}