我有一段tf1代码,摘自S. Nikolenko的一本好书《深度学习》。
这是一个简单的线性回归,分别将k
和b
分别学习到2和1。
%tensorflow_version 1.x
import numpy as np,tensorflow as tf
import pandas as pd
n_samples, batch_size, num_steps = 1000, 100, 20000 #set learning constants
X_data = np.random.uniform(1, 10, (n_samples, 1)) #generate array x from 1 to 10 of shape (1000,1)
print(X_data.shape)
y_data = 2 * X_data + 1 + np.random.normal(0, 2, (n_samples, 1)) #generate right answer and add noise to it (to make it scatter)
X = tf.placeholder(tf.float32, shape=(batch_size, 1)) #defining placeholders to put into session.run
y = tf.placeholder(tf.float32, shape=(batch_size, 1))
with tf.variable_scope('linear-regression'):
k = tf.Variable(tf.random_normal((1, 1)), name='slope') #defining 2 variables with shape (1,1)
b = tf.Variable(tf.zeros((1,)), name='bias') # and (1,)
print(k.shape,b.shape)
y_pred = tf.matmul(X, k) + b # all predicted y in batch, represents linear formula k*x + b
loss = tf.reduce_sum((y - y_pred) ** 2) # mean square
optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
display_step = 100
with tf.Session() as sess:
sess.run(tf.initialize_variables([k,b]))
for i in range(num_steps):
indices = np.random.choice(n_samples, batch_size) # taking random indices
X_batch, y_batch = X_data[indices], y_data[indices] # taking x and y from generated examples
_, loss_val, k_val, b_val = sess.run([optimizer, loss, k, b ],
feed_dict = { X : X_batch, y : y_batch })
if (i+1) % display_step == 0:
print('Epoch %d: %.8f, k=%.4f, b=%.4f' %
(i+1, loss_val, k_val, b_val))
我正在努力将其移植到TensorFlow 2
而且很长一段时间以来,我无法束手无策,而不是sess.run()
和feed_dict
(在幕后做魔术),官方文档详细介绍了编写模型类和等等,但我想尽量保持平坦。
还建议使用tf.GradientTape
计算导数,但我正努力将其正确地应用于示例
%tensorflow_version 2.x
import numpy as np,tensorflow as tf
import pandas as pd
n_samples, batch_size, num_steps = 1000, 100, 200
X_data = np.random.uniform(1, 10, (n_samples, 1))
y_data = 2 * X_data + 1 + np.random.normal(0, 2, (n_samples, 1))
X = tf.Variable(tf.zeros((batch_size, 1)), dtype=tf.float32, shape=(batch_size, 1))
y = tf.Variable(tf.zeros((batch_size, 1)), dtype=tf.float32, shape=(batch_size, 1))
k = tf.Variable(tf.random.normal((1, 1)), name='slope')
b = tf.Variable(tf.zeros((1,)), name='bias')
loss = lambda: tf.reduce_sum((y - (tf.matmul(X, k) + b)) ** 2)
optimizer = tf.keras.optimizers.SGD(0.01).minimize(loss, [k, b, X, y])
display_step = 100
for i in range(num_steps):
indices = np.random.choice(n_samples, batch_size)
X_batch, y_batch = X_data[indices], y_data[indices]
我需要SGD优化器最小化给定的损失函数并学习k和b值,从这一点上如何实现?
答案 0 :(得分:0)
经过大量的手册后,我知道该怎么办了,但隐藏在tf1的import ast
raw = "[[[1 2 3]\n [2 3 4]]]"
arr_str = raw.replace(" ", ",").replace("\n","")
arr = np.array(ast.literal_eval(arr_str))
里面,但是没有优化程序:
sess.run
和k
b