# 概要

kerasのbackendだけで、学習して推定してみた。

# 環境

Raspberry Pi 3 model B v1.2 element14
2017-09-07-raspbian-stretch
tensorflow-1.3

# サンプルコード

```from tensorflow.contrib.keras.python.keras import backend as K
from tensorflow.contrib.keras.python.keras.optimizers import SGD
import numpy as np
import matplotlib.pyplot as plt

dx = np.arange(-3, 3, 0.1)
dy = np.sin(dx)
input_dim = 1
output_dim = 1
hidden_dim = 8
x = K.placeholder(shape = (None, input_dim), name = "x")
ytrue = K.placeholder(shape = (None, output_dim), name = "y")
W1 = K.random_uniform_variable((input_dim, hidden_dim), 0, 1, name = "W1")
W2 = K.random_uniform_variable((hidden_dim, output_dim), 0, 1, name = "W2")
b1 = K.random_uniform_variable((hidden_dim, ), 0, 1, name = "b1")
b2 = K.random_uniform_variable((output_dim, ), 0, 1, name = "b2")
params = [W1, b1, W2, b2]
hidden = K.tanh(K.dot(x, W1) + b1)
ypred = K.tanh(K.dot(hidden, W2) + b2)
loss = K.mean(K.square(ypred - ytrue), axis = -1)
opt = SGD()
train = K.function(inputs = [x, ytrue], outputs = [W1, b2, loss], updates = updates)
for ep in range(2000):
for i in range(60):
c1, c2, c3 = train([[[dx[i]]], [[dy[i]]]])
if ep % 100 == 0:
print (ep, c3[0])
pred = K.function(inputs = [x], outputs = [ypred])
p = []
for i in range(60):
preds = pred([[[dx[i]]]])
p.append(preds[0][0])
print (p)
plt.plot(dx, dy, 'b', dx, p, 'r--')
plt.savefig("backend10.png")
plt.show()

```