我尝试使用tensorflow进行多元回归。 在此代码中使用的假数据和将应用于此模型的实际数据遵循双变量分布,因此我想找到二元分布的sigma1,sigma2,mux,muy。
这是描述双变量分布的链接 http://mathworld.wolfram.com/BivariateNormalDistribution.html
input$dataset
结果
import math as m
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from mpl_toolkits.mplot3d import Axes3D
num_points = 40
vectors_set = []
sigma_x1 = 0.01
sigma_y1 = 0.02
mu_x1 = 30
mu_y1 = 120
weight1 = 0.01
# create fake data
def normalf(x, y):
return weight1 / (2 * m.pi * sigma_x1 * sigma_y1) * m.exp(
-(x - mu_x1) * (x - mu_x1) / (2 * sigma_x1) - (y - mu_y1) * (y - mu_y1) / (2 * sigma_y1))
for i in range(num_points):
x1 = np.random.normal(30, 0.1)
y1 = np.random.normal(120, 0.1)
z1 = normalf(x1, y1)
vectors_set.append([x1, y1, z1])
x_data = [v[0] for v in vectors_set]
y_data = [v[1] for v in vectors_set]
z_data = [v[2] for v in vectors_set]
print('x_data :', x_data)
print('y_data :', y_data)
print('z_data :', z_data)
fig = plt.figure()
ax = Axes3D(fig)
for i in range(40):
xs = x_data[i]
ys = y_data[i]
zs = z_data[i]
ax.scatter(xs, ys, zs, color='blue')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
plt.show()
# train
sigma_x = tf.Variable(tf.random_uniform([1],0,1), trainable=True)
sigma_y = tf.Variable(tf.random_uniform([1],0,1), trainable=True)
mu_x = tf.Variable(tf.random_uniform([1], 129, 131), trainable=True)
mu_y = tf.Variable(tf.random_uniform([1], 29, 31), trainable=True)
weight = tf.Variable(tf.random_uniform([1],0,1), trainable=True)
var_x_data = tf.Variable(x_data)
var_y_data = tf.Variable(y_data)
z = weight/(2 * m.pi * sigma_x * sigma_y)*tf.exp(-(x_data - mu_x)*(x_data - mu_x)/(2 * sigma_x)-(y_data - mu_y)*(y_data - mu_y)/(2 * sigma_y))
loss = tf.reduce_mean(tf.square(z - z_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(10):
sess.run(train)
print('step :', step, '-', sess.run(sigma_x), sess.run(sigma_y), sess.run(mu_x), sess.run(mu_y), sess.run(weight))
答案 0 :(得分:0)
看起来您正在将sigma_x和sigma_y初始化为零。这将导致在第一次重量更新时除以零,并且所有内容都将设置为nan。最好的办法是用高差异进行初始化,然后从那里开始适应。