- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建一个程序,使用 Tensorflow 将一个点分类为 1
或 0
.我正在尝试围绕该图的中心创建一个椭圆形,其中蓝点是:
椭圆形中的所有内容都应归类为 1
,其他所有内容均应归类为 0
。在上图中,蓝点是 1
,红色 x 是 0
。
但是,每次我尝试对一个点进行分类时,它总是选择 1
,即使它是我用来训练它的点,说它是 0
。
我的问题很简单:为什么猜测总是1
,我做错了什么或者应该采取不同的方式来解决这个问题?这是我在没有教程的情况下尝试的第一个机器学习问题,所以我对这些东西真的了解不多。
如果您能提供任何帮助,我将不胜感激,谢谢!
这是我的代码:
#!/usr/bin/env python3
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
training_in = numpy.array([[0, 0], [1, 1], [2, 0], [-2, 0], [-1, -1], [-1, 1], [-1.5, 1], [3, 3], [3, 0], [-3, 0], [0, -3], [-1, 3], [1, -2], [-2, -1.5]])
training_out = numpy.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0])
def transform_data(x):
return [x[0], x[1], x[0]**2, x[1]**2, x[0]*x[1]]
new_training_in = numpy.apply_along_axis(transform_data, 1, training_in)
feature_count = new_training_in.shape[1]
x = tf.placeholder(tf.float32, [None, feature_count])
y = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.zeros([feature_count, 1]))
b = tf.Variable(tf.zeros([1]))
guess = tf.nn.softmax(tf.matmul(x, W) + b)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(tf.matmul(x, W) + b, y))
opti = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
for (item_x, item_y) in zip(new_training_in, training_out):
sess.run(opti, feed_dict={ x: [item_x], y: [[item_y]]})
print(sess.run(W))
print(sess.run(b))
plt.plot(training_in[:6, 0], training_in[:6, 1], 'bo')
plt.plot(training_in[6:, 0], training_in[6:, 1], 'rx')
results = sess.run(guess, feed_dict={ x: new_training_in })
for i in range(training_in.shape[0]):
xx = [training_in[i:,0]]
yy = [training_in[i:,1]]
res = results[i]
# this always prints `[ 1.]`
print(res)
# uncomment these lines to see the guesses
# if res[0] == 0:
# plt.plot(xx, yy, 'c+')
# else:
# plt.plot(xx, yy, 'g+')
plt.show()
最佳答案
使用softmax_cross_entropy_with_logits时出现问题.在您的具体情况下,logits
和 labels
都应具有形状 [batch_size, number_of_labels=2]
。
请注意,您的张量 logits=tf.matmul(x, W) + b
和 labels=y
的形状为 [batch_size, 1]
,因此 Tensorflow 假设 number_of_labels=1
。这就是为什么您的猜测总是相同的。
A) 您可以通过将 training_out
编码为单热向量来解决此问题。我建议使用 np.eye()
实现这一目标:
training_out = [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
training_out = numpy.eye(2)[training_out]
然后,您需要进行以下更改:
y = tf.placeholder(tf.float32, [None, 2])
W = tf.Variable(tf.zeros([feature_count, 2]))
b = tf.Variable(tf.zeros([2]))
...
for i in range(1000):
for (item_x, item_y) in zip(new_training_in, training_out):
sess.run(opti, feed_dict={x: [item_x], y: [item_y]})
...
results = sess.run(guess, feed_dict={x: new_training_in})[:,1]
B) 或者,您可以使用 sparse_softmax_cross_entropy_with_logits ,它允许 labels
具有形状 [batch_size]
。我已经调整了您的代码以使其以这种方式工作:
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
training_in = numpy.array(
[[0, 0], [1, 1], [2, 0], [-2, 0], [-1, -1], [-1, 1], [-1.5, 1], [3, 3], [3, 0], [-3, 0], [0, -3], [-1, 3], [1, -2],
[-2, -1.5]])
training_out = numpy.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0])
def transform_data(x):
return [x[0], x[1], x[0] ** 2, x[1] ** 2, x[0] * x[1]]
new_training_in = numpy.apply_along_axis(transform_data, 1, training_in)
feature_count = new_training_in.shape[1]
x = tf.placeholder(tf.float32, [None, feature_count])
y = tf.placeholder(tf.int32, [None])
W = tf.Variable(tf.zeros([feature_count, 2]))
b = tf.Variable(tf.zeros([2]))
guess = tf.nn.softmax(tf.matmul(x, W) + b)
cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.matmul(x, W) + b, y))
opti = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
for (item_x, item_y) in zip(new_training_in, training_out):
sess.run(opti, feed_dict={x: [item_x], y: [item_y]})
print(sess.run(W))
print(sess.run(b))
plt.plot(training_in[:6, 0], training_in[:6, 1], 'bo')
plt.plot(training_in[6:, 0], training_in[6:, 1], 'rx')
results = sess.run(guess, feed_dict={x: new_training_in})
for i in range(training_in.shape[0]):
xx = [training_in[i:, 0]]
yy = [training_in[i:, 1]]
res = results[i]
# this always prints `[ 1.]`
print(res)
# uncomment these lines to see the guesses
if res[0] == 0:
plt.plot(xx, yy, 'c+')
else:
plt.plot(xx, yy, 'g+')
plt.show()
关于python - Tensorflow 多变量逻辑回归不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39113871/
我想将模型及其各自训练的权重从 tensorflow.js 转换为标准 tensorflow,但无法弄清楚如何做到这一点,tensorflow.js 的文档对此没有任何说明 我有一个 manifest
我有一个运行良好的 TF 模型,它是用 Python 和 TFlearn 构建的。有没有办法在另一个系统上运行这个模型而不安装 Tensorflow?它已经经过预训练,所以我只需要通过它运行数据。 我
当执行 tensorflow_model_server 二进制文件时,它需要一个模型名称命令行参数,model_name。 如何在训练期间指定模型名称,以便在运行 tensorflow_model_s
我一直在 R 中使用标准包进行生存分析。我知道如何在 TensorFlow 中处理分类问题,例如逻辑回归,但我很难将其映射到生存分析问题。在某种程度上,您有两个输出向量而不是一个输出向量(time_t
Torch7 has a library for generating Gaussian Kernels在一个固定的支持。 Tensorflow 中有什么可比的吗?我看到 these distribu
在Keras中我们可以简单的添加回调,如下所示: self.model.fit(X_train,y_train,callbacks=[Custom_callback]) 回调在doc中定义,但我找不到
我正在寻找一种在 tensorflow 中有条件打印节点的方法,使用下面的示例代码行,其中每 10 个循环计数,它应该在控制台中打印一些东西。但这对我不起作用。谁能建议? 谢谢,哈米德雷萨, epsi
我想使用 tensorflow object detection API 创建我自己的 .tfrecord 文件,并将它们用于训练。该记录将是原始数据集的子集,因此模型将仅检测特定类别。我不明白也无法
我在 TensorFlow 中训练了一个聊天机器人,想保存模型以便使用 TensorFlow.js 将其部署到 Web。我有以下内容 checkpoint = "./chatbot_weights.c
我最近开始学习 Tensorflow,特别是我想使用卷积神经网络进行图像分类。我一直在看官方仓库中的android demo,特别是这个例子:https://github.com/tensorflow
我目前正在研究单图像超分辨率,并且我设法卡住了现有的检查点文件并将其转换为 tensorflow lite。但是,使用 .tflite 文件执行推理时,对一张图像进行上采样所需的时间至少是使用 .ck
我注意到 tensorflow 的 api 中已经有批量标准化函数。我不明白的一件事是如何更改训练和测试之间的程序? 批量归一化在测试和训练期间的作用不同。具体来说,在训练期间使用固定的均值和方差。
我创建了一个模型,该模型将 Mobilenet V2 应用于 Google colab 中的卷积基础层。然后我使用这个命令转换它: path_to_h5 = working_dir + '/Tenso
代码取自:- http://adventuresinmachinelearning.com/python-tensorflow-tutorial/ import tensorflow as tf fr
好了,所以我准备在Tensorflow中运行 tf.nn.softmax_cross_entropy_with_logits() 函数。 据我了解,“logit”应该是概率的张量,每个对应于某个像素的
tensorflow 服务构建依赖于大型 tensorflow ;但我已经成功构建了 tensorflow。所以我想用它。我做这些事情:我更改了 tensorflow 服务 WORKSPACE(org
Tensoflow 嵌入层 ( https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding ) 易于使用, 并且有大量的文
我正在尝试使用非常大的数据集(比我的内存大得多)训练 Tensorflow 模型。 为了充分利用所有可用的训练数据,我正在考虑将它们分成几个小的“分片”,并一次在一个分片上进行训练。 经过一番研究,我
根据 Sutton 的书 - Reinforcement Learning: An Introduction,网络权重的更新方程为: 其中 et 是资格轨迹。 这类似于带有额外 et 的梯度下降更新。
如何根据条件选择执行图表的一部分? 我的网络有一部分只有在 feed_dict 中提供占位符值时才会执行.如果未提供该值,则采用备用路径。我该如何使用 tensorflow 来实现它? 以下是我的代码
我是一名优秀的程序员,十分优秀!