예제 #1
0
def helper_test_coupling(my_activation, tf_activation, loss, inputs, y_true,
                         units):
    tf.random.set_seed(42)
    tf_layer = Dense(units, activation=tf_activation)
    tf_layer.build(inputs.shape)

    with tf.GradientTape(persistent=True) as tape:
        tape.watch([inputs, *tf_layer.trainable_weights])
        pred_tf = tf_layer(inputs)
        loss = loss(y_true, pred_tf)

    *grads_tf, dY = tape.gradient(
        loss, [inputs, *tf_layer.trainable_weights, pred_tf])

    tf.random.set_seed(42)
    my_layer = NN.Layer(units, my_activation)
    my_layer.build(inputs.shape)

    pred_my = my_layer(inputs)

    dX, [dW, dB] = my_layer.backprop(dY)
    grads_my = [dX, dW, dB]

    assert np.allclose(pred_my, pred_tf)

    assert all(
        np.allclose(grad_my, grad_tf)
        for grad_my, grad_tf in zip(grads_my, grads_tf))
예제 #2
0
# %%
X, Y = datasets.make_moons(n_samples=6_000, noise=0.3, random_state=0)

# %%

tf.random.set_seed(42)

loss = losses.BinaryCrossentropy()

dataset = (tf.data.Dataset.from_tensor_slices(
    (tf.cast(X, dtype=tf.float32),
     tf.cast(Y, dtype=tf.int32))).shuffle(X.shape[0]).batch(64))

clf = NN.NeuralNet()
clf.add(NN.Layer(units=3, activation=activations.ReLU()))
clf.add(NN.Layer(units=3, activation=activations.ReLU()))
clf.add(NN.Layer(units=3, activation=activations.ReLU()))
clf.add(NN.Layer(units=1, activation=activations.Sigmoid()))

train(
    NN=clf,
    dataset=dataset,
    loss=loss,
    optimizer=optimization.Adam(),
    epochs=50,
    use_tape=False,
)

plot_decision(clf, X, Y)