import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 使用numpy生成200個隨機點
x_data = np.linspace(-0.5, 0.5, 200)[:, np.newaxis]
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = np.square(x_data) + noise
# 定義兩個placeholder
x = tf.placeholder(tf.float32, [none, 1])
y = tf.placeholder(tf.float32, [none, 1])
# 定義神經網路的中間層
weights_l1 = tf.variable(tf.random_normal([1, 10]))
biases_l1 = tf.variable(tf.zeros([1, 10]))
wx_plus_b_l1 = tf.matmul(x, weights_l1) + biases_l1
l1 = tf.nn.tanh(wx_plus_b_l1)
# 定義神經網路輸出層
weights_l2 = tf.variable(tf.random_normal([10, 1]))
biases_l2 = tf.variable(tf.zeros([1, 1]))
wx_plus_b_l2 = tf.matmul(l1, weights_l2) + biases_l2
prediction = tf.nn.tanh(wx_plus_b_l2)
# 二次代價函式
loss = tf.reduce_mean(tf.square(y - prediction))
# 梯度下降法訓練
train_step = tf.train.gradientdescentoptimizer(0.1).minimize(loss)
#定義會話
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(2000):
sess.run(train_step, feed_dict=)
# 獲取**值
prediction_value = sess.run(prediction, feed_dict=)
# 畫圖
Keras 實現非線性回歸
import keras import numpy as np import matplotlib.pyplot as plt sequential按順序構成的模型 from keras.models import sequential dense全連線層 from keras.layers imp...
keras非線性回歸實現
這次非線性回歸實現,相比較於線性回歸,多新增了隱藏層層數,並且需要使用不同型別的啟用函式來進行適應非線性,分別使用了tanh和relu來顯示結果 非線性回歸 import keras import numpy as np import matplotlib.pyplot as plt 按順序構成的模...
tensorflow非線性回歸
該程式有輸入層,中間層和輸出層 執行環境 ubuntun menpo queen queen x550ld downloads py python nonliner regression.py coding utf 8 定義乙個神經網路 輸入層乙個元素,中間層10個神經元,輸出層1個元素 impor...