kerass 회귀sin을 사용하지 않음

2029 단어 Python3귀환

개요


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()


이상.

좋은 웹페이지 즐겨찾기