假设我有以下代码:
x = tf.placeholder("float32", shape=[None, ins_size**2*3], name = "x_input")
condition = tf.placeholder("int32", shape=[1, 1], name = "condition")
W = tf.Variable(tf.zeros([ins_size**2*3,label_option]), name = "weights")
b = tf.Variable(tf.zeros([label_option]), name = "bias")
if condition > 0:
y = tf.nn.softmax(tf.matmul(x, W) + b)
else:
y = tf.nn.softmax(tf.matmul(x, W) - b)
Run Code Online (Sandbox Code Playgroud)
该if
陈述是否会在计算中起作用(我不这么认为)?如果没有,我如何if
在TensorFlow计算图中添加一个语句?
我正在尝试用不平衡的数据训练网络.我有A(198个样本),B个(436个样本),C个(710个样本),D个(272个样本),我读过"weighted_cross_entropy_with_logits"但我发现的所有例子都是二进制分类所以我不是很对如何设置这些重量充满信心.
样本总数:1616
A_weight:198/1616 = 0.12?
如果我理解的话,背后的想法是惩罚少数民族阶级的错误,并且更加积极地评价少数民族中的命中,对吧?
我的代码:
weights = tf.constant([0.12, 0.26, 0.43, 0.17])
cost = tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(logits=pred, targets=y, pos_weight=weights))
Run Code Online (Sandbox Code Playgroud)
我已经阅读了这个和其他二进制分类的例子,但仍然不是很清楚.
提前致谢.
既然TensorFlow 1.1支持Keras API tf.contrib.keras
,如果我打算使用带有TF后端的Keras,我应该使用哪一个?
是tf.contrib.keras
版本以任何方式比普通Keras分布有什么不同?(想到TF特定的内部数据结构优化).如果我使用其中一种,那么将Keras和TensorFlow Core一起使用会有什么好处吗?
或者tf.contrib.keras
只是与Keras相同的代码库的副本,但在不同的命名空间下?
根据标准,
如果类X的定义没有显式地声明一个移动构造函数,那么当且仅当一个移动构造函数被隐式声明为默认值时
- X没有用户声明的复制构造函数,
- X没有用户声明的复制赋值运算符,
- X没有用户声明的移动赋值运算符,和
- X没有用户声明的析构函数.
现在以下无法编译
# include <utility>
class Foo
{
public:
Foo() = default;
Foo(Foo const &) = delete;
};
int main()
{
Foo f;
Foo g(std::move(f)); // compilation fails here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以似乎删除的函数被认为是用户定义的,这是有道理的(它不是它的默认实现).但是,在那种特殊情况下,如何删除复制construtor/assignment mess默认移动构造函数/赋值?
我认为这个问题具有实际意义,因为手动生成和特别是 这种默认函数的维护是容易出错的,同时,诸如std::unique_ptr
类成员之类的类的使用的(正当的)增加使得不可复制的类比以前更常见.
我是神经网络和TensorFlow的初学者,我正在努力理解它的作用arg_scope
.
在我看来,这是一种将"你想要做的事情"的字典放在一个具有某些变量的特定图层的方法.如果我错了,请纠正我.你会如何向初学者解释它究竟是什么?
我写了一个张量流CNN,它已经训练好了.我希望恢复它以便在几个样本上运行它但不幸的是它吐出来:
ValueError:没有要保存的变量
我的评估代码可以在这里找到:
import tensorflow as tf
import main
import Process
import Input
eval_dir = "/Users/Zanhuang/Desktop/NNP/model.ckpt-30"
checkpoint_dir = "/Users/Zanhuang/Desktop/NNP/checkpoint"
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()
def evaluate():
with tf.Graph().as_default() as g:
sess.run(init_op)
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
saver.restore(sess, eval_dir)
images, labels = Process.eval_inputs(eval_data = eval_data)
forward_propgation_results = Process.forward_propagation(images)
top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)
print(top_k_op)
def main(argv=None):
evaluate()
if __name__ == '__main__':
tf.app.run()
Run Code Online (Sandbox Code Playgroud) 在性能指南建议做对CPU,而该GPU的预处理.列出的原因是
我不确定是否理解这两个论点.
我当然假设预处理不会导致数据大小的急剧减少(例如,超级采样或裁剪到更小的尺寸),在这种情况下,到设备的传输时间的增益是显而易见的.我认为这些是相当极端的情况,并不构成上述建议的基础.
有人能理解这个吗?
我试图在TensorFlow中将F1得分定义为自定义指标DNNClassifier
.为此,我写了一个函数
def metric_fn(predictions=[], labels=[], weights=[]):
P, _ = tf.contrib.metrics.streaming_precision(predictions, labels)
R, _ = tf.contrib.metrics.streaming_recall(predictions, labels)
if P + R == 0:
return 0
return 2*(P*R)/(P+R)
Run Code Online (Sandbox Code Playgroud)
使用streaming_precision
和streaming_recall
来自TensorFlow来计算F1得分.之后,我为validation_metrics创建了一个新条目:
validation_metrics = {
"accuracy":
tf.contrib.learn.MetricSpec(
metric_fn=tf.contrib.metrics.streaming_accuracy,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
"precision":
tf.contrib.learn.MetricSpec(
metric_fn=tf.contrib.metrics.streaming_precision,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
"recall":
tf.contrib.learn.MetricSpec(
metric_fn=tf.contrib.metrics.streaming_recall,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
"f1score":
tf.contrib.learn.MetricSpec(
metric_fn=metric_fn,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES)
}
Run Code Online (Sandbox Code Playgroud)
但是,虽然我得到了正确的精度和召回值,但f1score
总是nan
:
INFO:tensorflow:Saving dict for global step 151: accuracy = 0.982456, accuracy/baseline_label_mean = 0.397661, accuracy/threshold_0.500000_mean = 0.982456, auc = 0.982867, f1score = nan, global_step = …
Run Code Online (Sandbox Code Playgroud) init_op = tf.global_variables_initializer()
Run Code Online (Sandbox Code Playgroud)
在定义任何变量或操作之前,然后得到一个错误
Attempting to use uninitialized value
Run Code Online (Sandbox Code Playgroud)
这里有一个解释,但它没有提到底层tf.global_variables_initializer
调用.它几乎复制TF API批发.这个问题集中在一些用户呼叫时仍然存在未初始化的值这一事实sess.run(init_op)
.示例代码和对什么tf.global_variables_initializer
是伟大的分析.
我注意到一些numpy操作采用了一个名为的参数shape
,例如np.zeros
,而其他一些操作则采用了一个名为的参数size
,例如np.random.randint
.对我来说,这些论点具有相同的功能,并且它们具有不同的名称这一事实有点令人困惑.实际上,size
似乎有点偏,因为它确实指定.shape
了输出.
有没有理由拥有不同的名字,即使它们最终都.shape
与输出相等,它们是否表达了不同的含义?