草庐IT

tf.gather

全部标签

python - Scikit Learn - 从特征数组的语料库而不是原始文档的语料库计算 TF-IDF

Scikit-Learn的TfidfVectorizer将原始文档集合转换为TF-IDF特征矩阵。我想将特征名称矩阵转换为TF-IDF特征,而不是原始文档。您输入fit_transform()的语料库应该是一组原始文档,但我希望能够将它(或类似函数)输入一组数组每个文档的功能。例如:corpus=[['orange','red','blue'],['orange','yellow','red'],['orange','green','purple(ifyoubelieveinpurple)'],['orange','reddishorange','blackandblue']]...与

python - Tensorflow 在使用 tf.device ('/cpu:0' 时分配 GPU 内存)

系统信息:1.1.0、GPU、Windows、Python3.5,代码在ipython控制台中运行。我正在尝试运行两个不同的Tensorflowsession,一个在GPU上(执行一些批处理工作),一个在我用于快速测试的CPU上,另一个运行。问题是,当我生成第二个session并指定withtf.device('/cpu:0')时,该session会尝试分配GPU内存并使我的另一个session崩溃。我的代码:importosos.environ["CUDA_VISIBLE_DEVICES"]=""importtimeimporttensorflowastfwithtf.device(

python - Tensorflow: 'tf.get_default_session()` 在 sess=tf.Session() 为 None 之后

我试图找出为什么tf.get_default_session()总是返回None类型:importtensorflowastftf.reset_default_graph()init=tf.global_variables_initializer()sess=tf.Session()sess.run(init)default=tf.get_default_session()default==None#True我不知道为什么default=tf.get_default_session()是None因为我认为它应该返回上一个session。谁能弄清楚我的代码有什么问题?

python - TF-IDF 查找新文档和数据集之间的余弦相似度

我有一个产品数据集的TF-IDF矩阵:tfidf=TfidfVectorizer().fit_transform(words)其中words是描述列表。这会产生一个69258x22024矩阵。现在我想找出新产品与矩阵中的产品之间的余弦相似度,因为我需要找到与其最相似的10个产品。我使用与上面相同的方法对其进行矢量化。但是,我无法将矩阵相乘,因为它们的大小不同(新矩阵可能有6个字,所以是1x6矩阵),所以我需要制作一个列数与原始列数相同的TFIDFVectorizer。我该怎么做? 最佳答案 我已经找到了它的工作方式。您需要先将新文档

python - 如何使用 tf.estimator 返回预测和标签(使用 predict 或 eval 方法)?

我正在使用Tensorflow1.4。我创建了一个自定义的tf.estimator来进行分类,如下所示:defmodel_fn():#Someoperationshere[...]returntf.estimator.EstimatorSpec(mode=mode,predictions={"Preds":predictions},loss=cost,train_op=loss,eval_metric_ops=eval_metric_ops,training_hooks=[summary_hook])my_estimator=tf.estimator.Estimator(model_f

python - 为什么 tensorflow 中的随机数生成器 tf.random_uniform 比 numpy 等效项快得多

下面的代码是我用来测试性能的:importtimeimportnumpyasnpimporttensorflowastft=time.time()foriinrange(400):a=np.random.uniform(0,1,(1000,2000))print("np.random.uniform:{}seconds".format(time.time()-t))t=time.time()foriinrange(400):a=np.random.random((1000,2000))print("np.random.random:{}seconds".format(time.time

python - tf.app.flags 是做什么的?为什么我们需要那个?

这个问题在这里已经有了答案:What'sthepurposeoftf.app.flagsinTensorFlow?(5个答案)关闭6年前。我正在阅读包含以下代码的tensorflow教程文件fully_connected_feed.py。我不明白那些是什么意思。为什么我们需要那个?看起来它只是定义了一些全局变量。为什么不直接定义它们呢?任何帮助表示赞赏。谢谢flags=tf.app.flagsFLAGS=flags.FLAGSflags.DEFINE_float('learning_rate',0.01,'Initiallearningrate.')flags.DEFINE_integ

python - 如何按特定值过滤 tf.data.Dataset?

我通过读取TFRecords创建了一个数据集,我映射了值,我想过滤数据集的特定值,但由于结果是一个带有张量的字典,我无法获得张量的实际值或用tf.cond()/tf.equal检查它。我该怎么做?defmapping_func(serialized_example):feature={'label':tf.FixedLenFeature([1],tf.string)}features=tf.parse_single_example(serialized_example,features=feature)returnfeaturesdeffilter_func(features):#th

python - tf.nn.depthwise_conv2d 太慢了。正常吗?

我正在试用一个名为“FactorizedCNN”的最新arxiv作品,主要论证了空间分离卷积(depth-wiseconvolution),加上channel-wiselinearprojection(1x1conv),可以加速卷积运算。thisisthefigurefortheirconvlayerarchitecture我发现我可以使用tf.nn.depthwise_conv2d和1x1卷积,或者使用tf.nn.separable_conv2d来实现这个架构。下面是我的实现:#convfilterfordepthwiseconvolutiondepthwise_filter=tf.

python - TensorFlow 2.0 : how to group graph using tf. 喀拉斯? tf.name_scope/tf.variable_scope 不再使用了吗?

回到TensorFlowinception模块,通过使用tf.name_scope或tf将它们分组.variable_scope.利用这些运算符,我们能够方便地构造计算图,从而使TensorBoard的图View更容易解释。只是结构化组的一个例子:这对于调试复杂的架构非常方便。不幸的是,tf.keras似乎忽略了tf.name_scope并且tf.variable_scope在TensorFlow>=2.0中消失了。因此,像这样的解决方案......withtf.variable_scope("foo"):withtf.variable_scope("bar"):v=tf.get_va