title: 网络结构思考
date: 2019-10-08 11:42:34

tags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def add_layer(inputs, in_shape, out_shape, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_shape, out_shape]))
bais = tf.Variable(tf.zeros([1, out_shape])+0.1)
y_pred = tf.matmul(inputs, Weights) + bais
if activation_function is None:
outputs = y_pred
else:
outputs = activation_function(y_pred)

return outputs


##### 创建数据,x y 的 shape[300,1]


x_data = np.linspace(-1,1,300, dtype=np.float32)[:, np.newaxis]

noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
y_data = np.square(x_data) - 0.5 + noise

xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])

l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
predict = add_layer(l1, 10, 1, activation_function=None)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-predict),
reduction_indices=[1]
))

train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)
for i in range(1000):
# training
sess.run(train, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to see the step improvement
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))

我的数据shape

x.shape = (None, 588) # 有N条样本,每条588个特征,也对有588个y。
y.shape = (None, 588)

588个x对应588个y可以计算吗?

这样我的数据对应的不是一个Y值,换个思路说,1个x对应一个y可以计算。588个x对应一个y可以计算。588个x对应588个y可以计算吗?

维度扩展,在第三个维度,数据都是1。这样可以计算。