sgd_hw/p2.py

35 lines
944 B
Python
Raw Normal View History

2021-02-12 15:50:20 +09:00
from layer import *
import numpy as np
import pickle
2021-02-13 00:26:53 +09:00
"""
2021-02-12 15:50:20 +09:00
DIMENTION = 3
VAR_RANGE = 1
N = 10
ALPHA = 0.1
SIGMA = VAR_RANGE * ALPHA
gen: np.random.Generator = np.random.default_rng()
or_weight = (gen.uniform(high = VAR_RANGE,low = -VAR_RANGE,size=(1,DIMENTION)))
or_bias = (gen.uniform(high = VAR_RANGE, low = -VAR_RANGE,size=()))
print(or_bias)
input_x = gen.uniform(low = -VAR_RANGE,high=VAR_RANGE,size=(DIMENTION,N))
y = or_weight @ input_x + or_bias
error = gen.normal(0,SIGMA,size = (1,N))
y += error
print(y)
2021-02-13 00:26:53 +09:00
"""
2021-02-12 15:50:20 +09:00
2021-02-13 00:26:53 +09:00
input_var = Variable(np.array([[1,2,3],[1,5,0]]))
weight = Variable(np.array([[2],[-1],[1]]))
bias = Variable(np.array([1]))
v = relu((input_var @ weight) + bias)
#print(v.numpy())
#print(v.numpy().shape, np.array([[1,1]]).shape)
k = matmul(np.array([[1,1]]), v)
print(make_mermaid_graph(k))
2021-02-12 15:50:20 +09:00
2021-02-13 00:26:53 +09:00
print(f"result : {k.numpy()}")
k.backprop(np.ones(()))
2021-02-12 15:50:20 +09:00
print(f"grad input : {input_var.grad}, w : {weight.grad}, b : {bias.grad}")