kerass 회귀sin을 사용하지 않음
개요
kerass 회귀sin을 사용하지 않음
사진.
샘플 코드 import numpy as np
import matplotlib.pyplot as plt
def tanh(x):
return np.tanh(x)
def dtanh(x):
return 1.0 - x ** 2
class TLP:
def __init__(self, Input, Hidden, Output):
self.Input = Input + 1
self.Hidden = Hidden + 1
self.Output = Output
self.W1 = np.random.uniform(-1.0, 1.0, (self.Hidden, self.Input))
self.W2 = np.random.uniform(-1.0, 1.0, (self.Output, self.Hidden))
def fit(self, X, t, learning_rate = 0.1, epochs = 10000):
X = np.hstack([np.ones([X.shape[0], 1]), X])
t = np.array(t)
for k in range(epochs):
i = np.random.randint(X.shape[0])
x = X[i]
z = tanh(np.dot(self.W1, x))
y = tanh(np.dot(self.W2, z))
bias2 = y - t[i]
if k % 1000 == 0:
print (k, bias2)
bias1 = dtanh(z) * np.dot(self.W2.T, bias2)
x = np.atleast_2d(x)
bias1 = np.atleast_2d(bias1)
self.W1 -= learning_rate * np.dot(bias1.T, x)
z = np.atleast_2d(z)
bias2 = np.atleast_2d(bias2)
self.W2 -= learning_rate * np.dot(bias2.T, z)
def predict(self, x):
x = np.array(x)
x = np.insert(x, 0, 1)
z = tanh(np.dot(self.W1, x))
y = tanh(np.dot(self.W2, z))
return y
if __name__ == "__main__":
X = np.arange(-3, 3, 0.1)
X = np.reshape(X, (60, 1))
y = np.sin(X)
tlp = TLP(1, 8, 1)
tlp.fit(X, y, learning_rate = 0.1, epochs = 12001)
data = np.arange(-3, 3, 0.1)
p = []
for x in data:
pred = tlp.predict(x)
p.append(pred)
plt.plot(data, y, 'b', data, p, 'r--')
plt.savefig("sin11.png")
plt.show()
이상.
Reference
이 문제에 관하여(kerass 회귀sin을 사용하지 않음), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/ohisama@github/items/d9b032b9db467ed3e62f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)

샘플 코드 import numpy as np
import matplotlib.pyplot as plt
def tanh(x):
return np.tanh(x)
def dtanh(x):
return 1.0 - x ** 2
class TLP:
def __init__(self, Input, Hidden, Output):
self.Input = Input + 1
self.Hidden = Hidden + 1
self.Output = Output
self.W1 = np.random.uniform(-1.0, 1.0, (self.Hidden, self.Input))
self.W2 = np.random.uniform(-1.0, 1.0, (self.Output, self.Hidden))
def fit(self, X, t, learning_rate = 0.1, epochs = 10000):
X = np.hstack([np.ones([X.shape[0], 1]), X])
t = np.array(t)
for k in range(epochs):
i = np.random.randint(X.shape[0])
x = X[i]
z = tanh(np.dot(self.W1, x))
y = tanh(np.dot(self.W2, z))
bias2 = y - t[i]
if k % 1000 == 0:
print (k, bias2)
bias1 = dtanh(z) * np.dot(self.W2.T, bias2)
x = np.atleast_2d(x)
bias1 = np.atleast_2d(bias1)
self.W1 -= learning_rate * np.dot(bias1.T, x)
z = np.atleast_2d(z)
bias2 = np.atleast_2d(bias2)
self.W2 -= learning_rate * np.dot(bias2.T, z)
def predict(self, x):
x = np.array(x)
x = np.insert(x, 0, 1)
z = tanh(np.dot(self.W1, x))
y = tanh(np.dot(self.W2, z))
return y
if __name__ == "__main__":
X = np.arange(-3, 3, 0.1)
X = np.reshape(X, (60, 1))
y = np.sin(X)
tlp = TLP(1, 8, 1)
tlp.fit(X, y, learning_rate = 0.1, epochs = 12001)
data = np.arange(-3, 3, 0.1)
p = []
for x in data:
pred = tlp.predict(x)
p.append(pred)
plt.plot(data, y, 'b', data, p, 'r--')
plt.savefig("sin11.png")
plt.show()
이상.
Reference
이 문제에 관하여(kerass 회귀sin을 사용하지 않음), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/ohisama@github/items/d9b032b9db467ed3e62f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import numpy as np
import matplotlib.pyplot as plt
def tanh(x):
return np.tanh(x)
def dtanh(x):
return 1.0 - x ** 2
class TLP:
def __init__(self, Input, Hidden, Output):
self.Input = Input + 1
self.Hidden = Hidden + 1
self.Output = Output
self.W1 = np.random.uniform(-1.0, 1.0, (self.Hidden, self.Input))
self.W2 = np.random.uniform(-1.0, 1.0, (self.Output, self.Hidden))
def fit(self, X, t, learning_rate = 0.1, epochs = 10000):
X = np.hstack([np.ones([X.shape[0], 1]), X])
t = np.array(t)
for k in range(epochs):
i = np.random.randint(X.shape[0])
x = X[i]
z = tanh(np.dot(self.W1, x))
y = tanh(np.dot(self.W2, z))
bias2 = y - t[i]
if k % 1000 == 0:
print (k, bias2)
bias1 = dtanh(z) * np.dot(self.W2.T, bias2)
x = np.atleast_2d(x)
bias1 = np.atleast_2d(bias1)
self.W1 -= learning_rate * np.dot(bias1.T, x)
z = np.atleast_2d(z)
bias2 = np.atleast_2d(bias2)
self.W2 -= learning_rate * np.dot(bias2.T, z)
def predict(self, x):
x = np.array(x)
x = np.insert(x, 0, 1)
z = tanh(np.dot(self.W1, x))
y = tanh(np.dot(self.W2, z))
return y
if __name__ == "__main__":
X = np.arange(-3, 3, 0.1)
X = np.reshape(X, (60, 1))
y = np.sin(X)
tlp = TLP(1, 8, 1)
tlp.fit(X, y, learning_rate = 0.1, epochs = 12001)
data = np.arange(-3, 3, 0.1)
p = []
for x in data:
pred = tlp.predict(x)
p.append(pred)
plt.plot(data, y, 'b', data, p, 'r--')
plt.savefig("sin11.png")
plt.show()
Reference
이 문제에 관하여(kerass 회귀sin을 사용하지 않음), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ohisama@github/items/d9b032b9db467ed3e62f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)