# 机器学习是一种认识世界的方式：源自生活，用于生活！

matrix = np.array([[15000,12000,10000,8000],

[14000,11000,9000,7000],

[13000,10000,8000,6000]])

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

importnumpyasnp

importtorch

matrix = np.array([[15000,12000,10000,8000],

[14000,11000,9000,7000],

[13000,10000,8000,6000]])

col_avg = np.mean(matrix,axis=)

row_avg = np.mean(matrix,axis=1)

global_avg = np.mean(matrix)

# init x with random variable

x = np.ones([3,4]) * global_avg

# make x varible

optimizer = torch.optim.SGD([x_var],lr=0.1)

fortrain_stepinrange(500):

row_loss =

foriinrange(x_var.size()[]):

# print (x_var[i,:])

x_row_avg = torch.mean(x_var[i,:])

row_loss += (x_row_avg - row_avg[i]) **2

row_loss = row_loss / x_var.size()[]

col_loss =

forjinrange(x_var.size()[1]):

# print (x_var[i,:])

x_col_avg = torch.mean(x_var[:,j])

col_loss += (x_col_avg - col_avg[j]) **2

col_loss = col_loss / x_var.size()[1]

total_loss = col_loss + row_loss

iftrain_step %10==:

print(total_loss.data.numpy()[])

total_loss.backward()# 误差反向传播, 计算参数更新值

optimizer.step()

print(x_var)

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180910B0BAGJ00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2022-01-22

2022-01-22

2022-01-22

2022-01-22

2022-01-22

2022-01-22

2018-06-22

2018-06-04

2018-05-04

2022-01-22

2022-01-22

2022-01-22