- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为特定任务编写一个二元分类器,而不是在输出层使用 2 个神经元,我只想使用一个带有 S 形函数的神经元,如果它低于 0.5,则基本上输出 0 类,否则输出 1 类。
图像被加载、调整为 64x64 并展平,以创建问题的复制品)。数据加载的代码将出现在最后。我创建占位符。
x = tf.placeholder('float',[None, 64*64])
y = tf.placeholder('float',[None, 1])
并定义模型如下。
def create_model_linear(data):
fcl1_desc = {'weights': weight_variable([4096,128]), 'biases': bias_variable([128])}
fcl2_desc = {'weights': weight_variable([128,1]), 'biases': bias_variable([1])}
fc1 = tf.nn.relu(tf.matmul(data, fcl1_desc['weights']) + fcl1_desc['biases'])
fc2 = tf.nn.sigmoid(tf.matmul(fc1, fcl2_desc['weights']) + fcl2_desc['biases'])
return fc2
weight_variable
和 bias_variable
函数只返回给定形状的 tf.Variable()
。 (他们的代码也在最后。)
然后我定义训练函数如下。
def train(x, hm_epochs):
prediction = create_model_linear(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = y) )
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
batch_size = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(train_x):
start = i
end = i + batch_size
batch_x = train_x[start:end]
batch_y = train_y[start:end]
_, c = sess.run([optimizer, cost], feed_dict = {x:batch_x, y:batch_y})
epoch_loss += c
i+=batch_size
print('Epoch', epoch+1, 'completed out of', hm_epochs,'loss:',epoch_loss)
correct = tf.greater(prediction,[0.5])
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
i = 0
acc = []
while i < len(train_x):
acc +=[accuracy.eval({x:train_x[i:i+1000], y:train_y[i:i + 1000]})]
i+=1000
print sum(acc)/len(acc)
train(x, 10)
的输出是
('Epoch', 1, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 2, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 3, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 4, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 5, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 6, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 7, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 8, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 9, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 10, 'completed out of', 10, 'loss:', 0.0)
0.0 What am I missing?
这里是所有实用函数的 promise 代码:
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def getLabel(wordlabel):
if wordlabel == 'Class_A':
return [1]
elif wordlabel == 'Class_B':
return [0]
else:
return -1
def loadImages(pathToImgs):
images = []
labels = []
filenames = os.listdir(pathToImgs)
imgCount = 0
for i in tqdm(filenames):
wordlabel = i.split('_')[1]
oneHotLabel = getLabel(wordlabel)
img = cv2.imread(pathToImgs + i,cv2.IMREAD_GRAYSCALE)
if oneHotLabel != -1 and type(img) is np.ndarray:
images += [cv2.resize(img,(64,64)).flatten()]
labels += [oneHotLabel]
imgCount+=1
print imgCount
return (images,labels)
最佳答案
我认为您应该使用 tf.nn.sigmoid_cross_entropy_with_logits
而不是 tf.nn.softmax_cross_entropy_with_logits
因为您在输出层中使用了 sigmoid 和 1 个神经元。
您还需要从 create_model_linear
中的最后一层移除 sigmoid并且,您没有使用 y
标签,准确性必须采用以下形式。
correct = tf.equal(tf.greater(tf.nn.sigmoid(prediction),[0.5]),tf.cast(y,'bool'))
关于python - 使用单输出神经元tensorflow的神经网络时loss和accuracy都是0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45459042/
我是随机森林分类器的新手。我正在使用它对具有两个类别的数据集进行分类。- 特征数量为 512。- 数据比例为1:4。即,75%的数据来自第一类,25%来自第二类。- 我使用了 500 棵树。 分类器产
我正在使用 Keras 2.0 包为 Python 训练一个批处理的神经网络。以下是有关数据和训练参数的一些信息: #samples in train: 414934 #features: 59009
如果我有 2 个标签(1 和 0),并且在我通过 softmax 激活层传递我的 logits 之后,我得到如下内容: [[0.1, 0.9], [0.3, 0.7], [0.333, 0.667]]
我使用accuracy中的tf.metrics函数来解决多个分类问题,并使用logits作为输入。 我的模型输出如下: logits = [[0.1, 0.5, 0.4], [0.
许多资料都描述了计算平方根倒数的“神奇”方法,显然可以追溯到 Quake 游戏。维基百科上有一篇很好的文章:https://en.wikipedia.org/wiki/Fast_inverse_squ
我正在创建一个基于位置的提醒,EKReminder。 对于坐标,我使用 CLGeocoder 将地址转换为 CLLocation。 就提醒本身而言,我认为有两个因素决定了提醒将触发的“区域”(半径/圆
我想获得比函数中的地址范围更准确的地址(来自坐标) geoCoder.reverseGeocodeLocation(location, completionHandler: {} ) 在谷歌地图 AP
'accurate' 和 'precise' 和有什么不一样? 如果有区别,你能举个例子吗 一个准确但不准确的数字 一个精确但不准确的数字 一个既准确又精确的数字 谢谢! 最佳答案 精度是指一个数字传
我有一个简单的两行代码块,它根据在另一个数组中找到的最接近的元素将值添加到一个数组。由于它深埋在 MCMC 中,它被执行了数百万次,我需要它尽可能高效。 下面的代码可以工作,而且很容易解释。基本上:数
我正在尝试使用 TensorFlow(而不是 Keras)重现 Coursera ML 类(class)的 NN 练习。 我发现使用tf.metrics.accuracy计算准确度给出的结果低于我计算
我想了解从 tf.metrics.accuracy 返回的值 考虑这个最小的例子: predictions = tf.constant([[1,0,1], [1,0,1]], dtype=tf.int
我正在尝试了解 tf.metrics.accuracy 的工作原理。我想比较下面给出的函数的批量准确度结果 with tf.name_scope('Accuracy1'): corre
当我使用 Rose 库计算度量时,我得到了召回率、精度和 F1 的度量。然而,当我手动计算时,召回率和精确率测量有所不同。怎么会这样? install.packages("ROSE") library
我有一个容器,有多个 s 在里面,代表页面。当用户滚动浏览这些页面时,我想更新当前页码标签。为此,我发现我可以使用 :in-viewport 选择器。我试着按照这个example ,并想出了这个 j
在使用 TensorFlow Keras 构建分类器时,人们通常会在编译步骤中通过指定 metrics=['accuracy'] 来监控模型准确性: model = tf.keras.Model(..
我正在使用 Python、Tensorflow 和 Keras 在 450x450 RGB watch 正面图像上运行自动编码器(例如 watch_1 )。我的目标是使用自动编码器生成的这些图像的编码
coords.accuracy 是如何测量的,单位是什么? 目标是在查找位置时使用它来确定误差半径。 最佳答案 通常以米为单位表示。 Geolocation API specified by W3C说
在使用 HTML5 地理定位 API 时,除了纬度和经度之外,我还获得了“以米为单位的位置精度”。这到底是什么意思(米不是精度单位)? 我假设它应该被解读为“以 p 的概率请求是在返回位置的 a 米范
所以,我正在构建一个基本的 Keras 文本分类器,但无论我做什么,我都无法使验证准确率高于 49-50%(或更低)。我的训练准确率攀升非常正常,从 50% 左右开始,在 4-5 个 epoch 后攀
我收到以下错误,我不知道可能出了什么问题。 我将 R Studio 与适用于 Windows 8.1 的 3.1.3 版 R 一起使用,并使用 Caret 包进行数据挖掘。 我有以下训练数据: str
我是一名优秀的程序员,十分优秀!