按十位分类整数数据

2024-04-26 10:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想分类 如果输入数据小于200,则输出为(0,1) 如果输入数据大于200,则输出为(1,0)

输入值为顺序整数值,层为5。你知道吗

隐藏层使用sigmoid,最后一个隐藏层使用softmax函数

损失函数是降维均值和梯度下降训练

import numpy as np
import tensorflow as tf

def set_x_data():
    x_data = np.array([[50]
                     , [60]
                     , [70]
                     , [80]
                     , [90]
                     , [110]
                     , [120]
                     , [130]
                     , [140]
                     , [150]
                     , [160]
                     , [170]
                     , [180]
                     , [190]
                     , [200]
                     , [210]
                     , [220]
                     , [230]
                     , [240]
                     , [250]
                     , [260]
                     , [270]
                     , [280]
                     , [290]
                     , [300]
                     , [310]
                     , [320]
                     , [330]
                     , [340]
                     , [350]
                     , [360]
                     , [370]
                     , [380]
                     , [390]])

    return x_data

def set_y_data(x):
    y_data = np.array([[0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [0, 1]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]
                     , [1, 0]])
    return y_data

def set_bias(efficiency):
    arr = np.array([efficiency])

    return arr

W1 = tf.Variable(tf.random_normal([1, 5]), name='weight1')
W2 = tf.Variable(tf.random_normal([5, 5]), name='weight2')
W3 = tf.Variable(tf.random_normal([5, 5]), name='weight3')
W4 = tf.Variable(tf.random_normal([5, 5]), name='weight4')
W5 = tf.Variable(tf.random_normal([5, 2]), name='weight5')

def inference(input, b):
    hidden_layer1 = tf.sigmoid(tf.matmul(input, W1) + b)
    hidden_layer2 = tf.sigmoid(tf.matmul(hidden_layer1, W2) + b)
    hidden_layer3 = tf.sigmoid(tf.matmul(hidden_layer2, W3) + b)
    hidden_layer4 = tf.sigmoid(tf.matmul(hidden_layer3, W4) + b)
    out_layer = tf.nn.softmax(tf.matmul(hidden_layer4, W5) + b)

    return out_layer

def loss(hypothesis, y):
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), reduction_indices=[1]))

    return cross_entropy

def train(loss):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
    train = optimizer.minimize(loss)

    return train

x_data = set_x_data(1)
y_data = set_y_data(0)
b_data = set_bias(0.8)

x= tf.placeholder(tf.float32, shape=[None, 1])
y= tf.placeholder(tf.float32, shape=[None, 2])
b = tf.placeholder(tf.float32, shape=[None])

hypothesis = inference(x, b)
loss = loss(hypothesis, y)
train = train(loss)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

print(sess.run(W1))

for step in range(2000):
    sess.run(train, feed_dict={x:x_data, y:y_data, b:b_data})

print(sess.run(W1))
print(sess.run(hypothesis, feed_dict={x:np.array([[1000]]), b:b_data}))

当我在训练前和训练后打印W1时,值并没有特别改变,当输入=1000时测试,该值并没有达到我的预期。我认为值接近(1,0),但结果接近(0.5,0.5)

我猜错误来自损失函数,因为它是从这里和那里复制的,但我不能确定


上面的代码只是简化了我的代码,但我想我必须显示我真正的代码

代码太长,所以我创建了新的帖子

classifying data by tensorflow but accuracy value didn't change


Tags: datareturntfdefnptrainrandomvariable
1条回答
网友
1楼 · 发布于 2024-04-26 10:19:46

上述网络的训练中存在一些问题,但是只要做一些更改,就可以实现一个获得this decision function的网络

The plot in the link显示2级的分数,即如果x>;200)

此网络中有待改进的问题列表:

  • 培训数据非常缺乏(只有34分!)这通常太小,尤其是对于您的案例中的5层网络。在网络中,通常需要比参数多得多的输入样本。尝试添加更多的输入值并减少层的数量(如下面的代码所示-我使用了浮点而不是整数来获得更多的点,但我认为它仍然是兼容的)。

  • 输入范围通常需要缩放(下面我尝试了一个除以常数的超简单缩放)。这是因为您通常希望避免高范围的变量(特别是当您通过具有软最大非线性的许多层时,这将破坏包含在非常高或非常低的值中的信息)。在更高级的情况下,您可能需要执行最小-最大缩放或z-分数。

  • 尝试更多的时代(并尝试绘制损失函数值的演变)。在给定的历元数下,损失函数的优化没有收敛。下面我做了10倍多的纪元。看看下面的代码现在是如何在this plot中聚合的(看看2000个时代是如何不够的):

  • 有帮助的是对(x,y)数据进行洗牌。虽然这在这种情况下并不重要,但它收敛得更快(见Le Cun的论文“Efficient Backprop”)。在更严重的例子中,它通常是需要的。

  • 重要的是,我想你希望b是一个参数,而不是一个常数,不是吗?网络的偏差通常也与乘性权重一起优化。(另外,对所有隐藏层使用单一的共享偏移也是不常见的。)

下面是代码。注意,可能会有进一步的改进,但这些技巧最终会得到所需的决策函数。你知道吗

我添加了一些内联注释,以指示相对于原始的更改。我希望你觉得这些建议很有见地!你知道吗

代码:

import numpy as np
import tensorflow as tf

# I've modified the functions set_x_data and set_y_data
# so as to generate a larger set of numbers. 

# Generate a range of numbers from 50 to 390
def set_x_data():
    x_data = np.arange(50, 390, 0.1)
    return x_data[:,None]

# Assign labels depending on x_data
def set_y_data(x_data):
    ydata1 = x_data >= 200
    ydata2 = x_data < 200
    return np.hstack((ydata1, ydata2))

def set_bias(efficiency):
    arr = np.array([efficiency])

    return arr

# Let's keep W1 and W5 (one hidden layer only)
# BTW, in this problem you could do with 0 hidden layers. But keeping
# 1 to show it works
W1 = tf.Variable(tf.random_normal([1, 5]), name='weight1')
W5 = tf.Variable(tf.random_normal([5, 2]), name='weight5')

# BTW, b should be a parameter, too. 
b = tf.Variable(tf.constant(0.0))

# Just keeping 1 hidden layer
def inference(input):
    hidden_layer1 = tf.sigmoid(tf.matmul(input, W1) + b)
    out_layer = tf.nn.softmax(tf.matmul(hidden_layer1, W5) + b)

    return out_layer

# This is unchanged
def loss(hypothesis, y):
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), reduction_indices=[1]))

    return cross_entropy

# This is unchanged
def train(loss):
    optimizer = 
tf.train.GradientDescentOptimizer(learning_rate=0.1)
    train = optimizer.minimize(loss)

    return train

# Using SCALE to normalize the input variables (range of inputs too big)
# This is a simple normalization in this case. Other examples are 
# Min-Max normalization or z-scores. 

SCALE = 1000
x_data = set_x_data()
y_data = set_y_data(x_data)
x_data /= SCALE

# Now only placeholders are x and y (b is a parameter)
x= tf.placeholder(tf.float32, shape=[None, 1])
y= tf.placeholder(tf.float32, shape=[None, 2])

hypothesis = inference(x)
loss = loss(hypothesis, y)
train = train(loss)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

print(sess.run(W1))

# Epochs x 10, it did not converge with fewer epochs
epochs = 20000
losses = np.zeros(epochs)
for step in range(epochs):
# Shuffle data
    r = np.random.permutation(x_data.shape[0])
    x_data = x_data[r]
    y_data = y_data[r,:]
    # Small modification here to capture the loss. 
    _, l = sess.run([train, loss], feed_dict={x:x_data, y:y_data})
    losses[step] = l

print(sess.run(W1))
print(sess.run(b))

显示上述决策函数的代码:

%matplotlib inline
import matplotlib.pyplot as plt
ystar = np.arange(50, 400, 10)[:,None]
plt.plot(ystar, sess.run(hypothesis, feed_dict={x:ystar/SCALE})[:,0])

相关问题 更多 >