- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了这个神经网络,但每次运行它时,它都会给我带来不同的损失,并且在整个循环中该损失保持不变。我想为“xx”中的每 3 个值作为输入预测“yy”中的一个值。另外我如何显示我的输出?例如:我想显示一个预测值尽可能接近“yy”中值的数组。
import tensorflow as tf
xx=(
[178.72,218.38,171.1],
[211.57,215.63,173.13],
[196.25,196.69,116.91],
[121.88,132.07,85.02],
[117.04,135.44,112.54],
[118.13,124.04,97.98],
[116.73,125.88,99.04],
[118.75,125.01,110.16],
[109.69,111.72,69.07],
[76.57,96.88,67.38],
[91.69,128.43,87.57],
[117.57,146.43,117.57]
)
yy=(
[212.09],
[195.58],
[127.6],
[116.5],
[117.95],
[117.55],
[117.55],
[110.39],
[74.33],
[91.08],
[121.75],
[127.3]
)
x=tf.placeholder(tf.float32,[None,3])
y=tf.placeholder(tf.float32,[None,1])
n1=5
n2=5
classes=12
def neuralnetwork(data):
hl1={'weights':tf.Variable(tf.random_normal([3,n1])),'biases':tf.Variable(tf.random_normal([n1]))}
hl2={'weights':tf.Variable(tf.random_normal([n1,n2])),'biases':tf.Variable(tf.random_normal([n2]))}
op={'weights':tf.Variable(tf.random_normal([n2,classes])),'biases':tf.Variable(tf.random_normal([classes]))}
l1=tf.add(tf.matmul(data,hl1['weights']),hl1['biases'])
l1=tf.nn.relu(l1)
l2=tf.add(tf.matmul(l1,hl2['weights']),hl2['biases'])
l2=tf.nn.relu(l2)
output=tf.matmul(l2,op['weights'])+op['biases']
return output
def train(x):
pred=neuralnetwork(x)
# cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
sq = tf.square(pred-y)
loss=tf.reduce_mean(sq)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
#optimizer=tf.train.RMSPropOptimizer(0.01).minimize(cost)
epochs=100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(epochs):
epoch_loss=0
for i in range (int(1)):
batch_x=xx
batch_y=yy
# a=tf.shape(xx)
#print(sess.run(a))
c=sess.run(loss,feed_dict={x:batch_x, y: batch_y})
epoch_loss+=c
print("Epoch ",epoch," completed out of ",epochs, 'loss:', epoch_loss)
train(x)
最佳答案
我不确定你到底想要完成什么,但在我看来这是一个回归问题,而不是分类问题。我想下面的代码就是你想要的。我已经把它清理了一点,但仍然试图以你能认出的方式保留它。我个人会以不同的方式来写这个。
import tensorflow as tf
xx = (
[178.72, 218.38, 171.1],
[211.57, 215.63, 173.13],
[196.25, 196.69, 116.91],
[121.88, 132.07, 85.02],
[117.04, 135.44, 112.54],
[118.13, 124.04, 97.98],
[116.73, 125.88, 99.04],
[118.75, 125.01, 110.16],
[109.69, 111.72, 69.07],
[76.57, 96.88, 67.38],
[91.69, 128.43, 87.57],
[117.57, 146.43, 117.57]
)
yy = (212.09, 195.58, 127.6, 116.5, 117.95, 117.55, 117.55,
110.39, 74.33, 91.08, 121.75, 127.3)
x = tf.placeholder(tf.float32, [None, 3])
y = tf.placeholder(tf.float32, [None])
def neuralnetwork(data, n1=5, n2=5):
hl1 = {'weights': tf.Variable(tf.random_normal([3, n1])), 'biases':
tf.Variable(tf.random_normal([n1]))}
hl2 = {'weights': tf.Variable(tf.random_normal([n1, n2])),
'biases': tf.Variable(tf.random_normal([n2]))}
op = {'weights': tf.Variable(tf.random_normal([n2, 1])), 'biases':
tf.Variable(tf.random_normal([1]))}
l1 = tf.add(tf.matmul(data, hl1['weights']), hl1['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hl2['weights']), hl2['biases'])
l2 = tf.nn.relu(l2)
output = tf.matmul(l2, op['weights']) + op['biases']
return output
N_EPOCHS = 100
if __name__ == '__main__':
pred = neuralnetwork(x)
loss = tf.reduce_mean(tf.squared_difference(pred, y))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(N_EPOCHS):
epoch_loss = sess.run([train, loss], feed_dict={x: xx, y: yy})[1]
print("Epoch", epoch, " completed out of", N_EPOCHS, "loss:",
epoch_loss)
您犯了两个主要错误:
您尝试拥有 12 个输出节点,您可能想要的是单个节点,它尝试预测相应的 y 值。
您没有调用 train
操作,因此优化器实际上没有执行任何操作。
Also how can I show my output? For example: I want to show an array having predictions as close as possible to the values in 'yy'
例如这些行:
predictions = sess.run(pred, feed_dict={x: xx, y: yy})
print("Predictions:", predictions)
这将简单地评估使用整个数据集作为输入来计算 pred
张量所需的计算图部分,并将其输入占位符。
但是,正如您所看到的,无论输入如何,您的网络都会简单地学习预测标签的平均值。
关于python - Tensorflow:训练神经网络时损失没有改善,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49945462/
real adaboost Logit boost discrete adaboost 和 gentle adaboost in train cascade parameter 有什么区别.. -bt
我想为 book crossing 构建训练数据矩阵和测试数据矩阵数据集。但作为 ISBN 代码的图书 ID 可能包含字符。因此,我无法应用此代码(来自 tutorial ): #Create two
我找到了 JavaANPR 库,我想对其进行自定义以读取我所在国家/地区的车牌。 似乎包含的字母表与我们使用的字母表不同 ( http://en.wikipedia.org/wiki/FE-Schri
我有一个信用卡数据集,其中 98% 的交易是非欺诈交易,2% 是欺诈交易。 我一直在尝试在训练和测试拆分之前对多数类别进行欠采样,并在测试集上获得非常好的召回率和精度。 当我仅在训练集上进行欠采样并在
我打算: 在数据集上从头开始训练 NASNet 只重新训练 NASNet 的最后一层(迁移学习) 并比较它们的相对性能。从文档中我看到: keras.applications.nasnet.NASNe
我正在训练用于分割的 uNet 模型。训练模型后,输出全为零,我不明白为什么。 我看到建议我应该使用特定的损失函数,所以我使用了 dice 损失函数。这是因为黑色区域 (0) 比白色区域 (1) 大得
我想为新角色训练我现有的 tesseract 模型。我已经尝试过 上的教程 https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesser
我的机器中有两个 NVidia GPU,但我没有使用它们。 我的机器上运行了三个神经网络训练。当我尝试运行第四个时,脚本出现以下错误: my_user@my_machine:~/my_project/
我想在python的tensorflow中使用稀疏张量进行训练。我找到了很多代码如何做到这一点,但没有一个有效。 这里有一个示例代码来说明我的意思,它会抛出一个错误: import numpy as
我正在训练一个 keras 模型,它的最后一层是单个 sigmoid单元: output = Dense(units=1, activation='sigmoid') 我正在用一些训练数据训练这个模型
所以我需要使用我自己的数据集重新训练 Tiny YOLO。我正在使用的模型可以在这里找到:keras-yolo3 . 我开始训练并遇到多个优化器错误,添加了错误代码以防止混淆。 我注意到即使它应该使用
将 BERT 模型中的标记化范式更改为其他东西是否有意义?也许只是一个简单的单词标记化或字符级标记化? 最佳答案 这是论文“CharacterBERT: Reconciling ELMo and BE
假设我有一个非常简单的神经网络,比如多层感知器。对于每一层,激活函数都是 sigmoid 并且网络是全连接的。 在 TensorFlow 中,这可能是这样定义的: sess = tf.Inte
有没有办法在 PyBrain 中保存和恢复经过训练的神经网络,这样我每次运行脚本时都不必重新训练它? 最佳答案 PyBrain 的神经网络可以使用 python 内置的 pickle/cPickle
我尝试使用 Keras 训练一个对手写数字进行分类的 CNN 模型,但训练的准确度很低(低于 10%)并且误差很大。我尝试了一个简单的神经网络,但没有效果。 这是我的代码。 import tensor
我在 Windows 7 64 位上使用 tesseract 3.0.1。我用一种新语言训练图书馆。 我的示例数据间隔非常好。当我为每个角色的盒子定义坐标时,盒子紧贴角色有多重要?我使用其中一个插件,
如何对由 dropout 产生的许多变薄层进行平均?在测试阶段要使用哪些权重?我真的很困惑这个。因为每个变薄的层都会学习一组不同的权重。那么反向传播是为每个细化网络单独完成的吗?这些细化网络之间的权重
我尝试训练超正方语言。我正在使用 Tess4J 进行 OCR 处理。我使用jTessBoxEditor和SerakTesseractTrainer进行训练操作。准备好训练数据后,我将其放在 Tesse
我正在构建一个 Keras 模型,将数据分类为 3000 个不同的类别,我的训练数据由大量样本组成,因此在用一种热编码对训练输出进行编码后,数据非常大(item_count * 3000 * 的大小)
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!