Created
December 4, 2018 13:59
-
-
Save Koziev/fff81fd8354663c7cd15dda7a5e75b99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Решение задачи линейно регрессии по МНК с помощью Keras. | |
""" | |
from __future__ import print_function | |
import random | |
import numpy as np | |
from keras.layers import Dense, Input | |
from keras.models import Model | |
def calc_y(x): | |
y = 0.3 + 2.0*x + random.gauss(mu=0.0, sigma=0.000001) | |
return y | |
# Сформируем слегка зашумленный датасет | |
nb_samples = 100 | |
x_data = np.linspace(start=0.0, stop=1.0, num=nb_samples) | |
y_data = np.array(list(map(calc_y, x_data))) | |
# Строим простую сетку с одним линейным слоем. | |
input = Input(shape=(1,)) | |
d = Dense(units=1, ) | |
output = d(input) | |
model = Model(inputs=input, outputs=output) | |
model.compile(loss='mse', optimizer='rmsprop') | |
model.fit(x_data, y_data, nb_epoch=1000, batch_size=16, verbose=1) | |
# Посмотрим, какие веса для линейной функции подобрала сетка. | |
w = d.get_weights() | |
w1 = w[0][0, 0] | |
w2 = w[1][0] | |
print('w1={} w2={}'.format(w1, w2)) | |
print('All done.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment