Exemplo n.º 1
0
 def test_forward1(self):
     x = Variable(np.array([[1, 2, 3], [4, 5, 6]]))
     w = Variable(x.data.T)
     y = F.matmul(x, w)
     res = y.data
     expected = np.array([[14, 32], [32, 77]])
     self.assertTrue(array_allclose(res, expected))
Exemplo n.º 2
0
    def test_matmul(self):
        x = Variable(np.random.randn(2, 3))
        W = Variable(np.random.randn(3, 4))
        y = F.matmul(x, W)
        y.backward()

        self.assertEqual(x.grad.shape, (2, 3))
        self.assertEqual(W.grad.shape, (3, 4))
Exemplo n.º 3
0
    def test_matmul(self):

        x = Variable(np.array([[1, 1], [1, 1]]))
        W = Variable(np.array([[2], [2]]))
        y = matmul(x, W)
        y.backward()

        assert_array_equal(y.data, np.array([[4], [4]]))
        assert_array_equal(x.grad.data, np.array([[2, 2], [2, 2]]))
        assert_array_equal(W.grad.data, np.array([[2], [2]]))
Exemplo n.º 4
0
if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
from dezero import Variable
import dezero.functions as F
from dezero.utils import sum_to

# ベクトルの内積
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.dot(a, b)
print(c)
# 行列の積
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.dot(a, b)
print(c)

x = Variable(np.random.randn(2, 3))
W = Variable(np.random.randn(3, 4))
y = F.matmul(x, W)
y.backward()
print(x.grad.shape)
print(W.grad.shape)
Exemplo n.º 5
0
def predict(x):
    y = F.matmul(x, W) + b
    return y
Exemplo n.º 6
0
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
from dezero import Variable
import dezero.functions as F

x = Variable(np.random.randn(2, 3))
w = Variable(np.random.randn(3, 4))
t = F.matmul(x, w)
y = F.sum(t)
y.backward()

print(x.grad.shape)
print(w.grad.shape)
Exemplo n.º 7
0
 def test_backward(self):
     x = Variable(np.arange(4).reshape(2, 2))
     W = Variable(np.arange(6).reshape(2, 3))
     y = matmul(x, W)
     y.backward()
Exemplo n.º 8
0
 def test_forward(self):
     x = Variable(np.arange(4).reshape(2, 2))
     W = Variable(np.arange(6).reshape(2, 3))
     y = matmul(x, W)
     assert_equal(y.data, np.array([[3, 4, 5], [9, 14, 19]]))
Exemplo n.º 9
0
def predict(x):
    y = F.matmul(x, w1) + b1
    y = F.sigmoid(y)
    y = F.matmul(y, w2) + b2
    return y
Exemplo n.º 10
0
 def test_backward2(self):
     x_data = np.random.randn(10, 1)
     w_data = np.random.randn(1, 5)
     f = lambda w: F.matmul(Variable(x_data), w)
     self.assertTrue(gradient_check(f, w_data))
Exemplo n.º 11
0
 def test_backward1(self):
     x = np.random.randn(3, 2)
     w = np.random.randn(2, 3)
     f = lambda x: F.matmul(x, Variable(w))
     self.assertTrue(gradient_check(f, x))
Exemplo n.º 12
0
def gram_mat(x):
    N, C, H, W = x.shape
    features = x.reshape(C, -1)
    gram = F.matmul(features, features.T)
    return gram.reshape(1, C, C)
Exemplo n.º 13
0
def predict(x):
    return F.matmul(x, W) + b
import numpy as np
from dezero import Variable
import dezero.functions as F

# Inner products
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
a, b = Variable(a), Variable(b)  # Optional
c = F.matmul(a, b)
print(c)

# Matrix product
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = F.matmul(a, b)
print(c)
Exemplo n.º 15
0
    y = F.matmul(x, W) + b
    return y


def mean_squared_error(x0, x1):
    diff = x0 - x1
    return F.sum(diff**2) / len(diff)


lr = 0.1
iters = 100
for i in range(iters):
    y_pred = predict(x)
    loss = mean_squared_error(y, y_pred)

    W.clear_grad()
    b.clear_grad()
    loss.backward()

    W.data -= lr * W.grad.data
    b.data -= lr * b.grad.data
    print(W, b, loss)

from IPython import embed
embed()
sorted_x = sorted(x.data.squeeze())
predictions = sorted((F.matmul(x, W) + b).data.squeeze())
plt.scatter(x.data.squeeze(), y.data.squeeze())
plt.plot(sorted_x, predictions, color='r')
plt.show()