- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我认为这应该是一件非常简单的事情,但我没有解决它。我正在尝试对两个 senond 阶 Eigen 张量进行双收缩。一切正常,但双重收缩的结果是 Eigen
类型:
Eigen::TensorContractionOp<const std::array<Eigen::IndexPair<int>, 2ul>, const Eigen::TensorFixedSize<double, Eigen::Sizes<3l, 3l> >, const Eigen::TensorFixedSize<double, Eigen::Sizes<3l, 3l> > >
但我需要一个double
。我可以打印它,但我无法使用它。
代码如下
#include <iostream>
#include <unsupported/Eigen/CXX11/Tensor>
int main()
{
auto tensor1 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor1.setValues({ {1, 0, 0},
{0, 1, 0},
{0, 0, 1} });
std::cout << "tensor1:\n" << tensor1 << "\n";
auto tensor2 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor2.setValues({ {2, 0, 0},
{0, 2, 0},
{0, 0, 2} });
std::cout << "tensor2:\n" << tensor2 << "\n";
Eigen::array<Eigen::IndexPair<int>, 2> contraction_pair0011
= { Eigen::IndexPair<int>(0, 0), Eigen::IndexPair<int>(1, 1)};
auto tensor1_tensor2 = tensor1.contract(tensor2, contraction_pair0011);
std::cout << "tensor1 : tensor2:\n" << tensor1_tensor2 << "\n";
// double value = tensor1_tensor2; // won't compile
}
我需要一个函数或调用来获取结果的值,希望有人能帮助我。
干杯乔纳斯
最佳答案
我解决了这个问题,但认为如果您正在使用 Eigen::Tensor 模块,它也会对您有所帮助。
如所写here在 Tensor Operations 和 C++“auto” 部分:
因为张量运算创建张量运算符,C++ auto
关键字没有其直观的含义。当您使用 auto
您没有得到张量,而是一个未计算的表达式...
所以张量收缩的结果是
Eigen::TensorContractionOp<...>
而不是我们可以从中获取其元素的张量。所以我们需要知道结果张量的大小。问题是结果必须是用空 Eigen::Sizes<>
完成的标量张量。
Eigen::TensorFixedSize<double, Eigen::Sizes<>>
这里是运行代码。我希望它能帮助别人...
#include <iostream>
#include <unsupported/Eigen/CXX11/Tensor>
int main()
{
auto tensor1 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor1.setValues({ {1, 0, 0},
{0, 1, 0},
{0, 0, 1} });
std::cout << "tensor1:\n" << tensor1 << "\n";
auto tensor2 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor2.setValues({ {2, 0, 0},
{0, 2, 0},
{0, 0, 2} });
std::cout << "tensor2:\n" << tensor2 << "\n";
Eigen::array<Eigen::IndexPair<int>, 2> contraction_pair0011
= { Eigen::IndexPair<int>(0, 0), Eigen::IndexPair<int>(1, 1)};
Eigen::TensorFixedSize<double, Eigen::Sizes<>> tensor1_tensor2 = tensor1.contract(tensor2, contraction_pair0011);
std::cout << "tensor1 : tensor1:\n" << tensor1_tensor2 << "\n";
double t1_t2 = tensor1_tensor2(0);
std::cout << "result in double:\n" << t1_t2 << "\n";
}
关于c++ - Eigen::Tensor 双收缩到标量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45940313/
我试图将迁移学习应用于 InceptionV3。这是我的代码: inception_model = InceptionV3(weights='imagenet',include_top=False)
我正在尝试展示 GAN 网络在某些指定时期的结果。打印当前结果的功能以前与 TF 一起使用。我需要换成 pytorch。 def show_result(G_net, z_, num_epoch, s
我对孪生神经网络还很陌生,最近发现了 this example和 Colab notebook . 运行代码时出现以下错误: IndexError: invalid index of a 0-dim
我正在尝试使用在此 PR 中添加的“高级”、numpy 样式的切片,但是我遇到了 same issue as the user here : ValueError: Shape must be ran
我想在 TensorFlow 中做类似这段 Numpy 代码的事情: a = np.zeros([5, 2]) idx = np.random.randint(0, 2, (5,)) row_idx
我有以下特征张量: Eigen::Tensor m(3,10,10); 我想访问第一个矩阵。在 numpy 中我会这样做 m(0,:,:) 我如何在 Eigen 中做到这一点 最佳答案 您可以使用 .
1、问题 模型训练完后进行测试,报错 RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我应该如何从 Eigen::Tensor 创建一个 tensorflow::Tensor?我可以一个接一个地复制元素,但我希望有更好的方法。 最佳答案 没有公共(public) api 可以在不复制数
我正在尝试使用 Tensorflow(版本 0.9.0)以与 beginner's tutorial 非常相似的方式训练一个简单的二元逻辑回归分类器。并且在拟合模型时遇到以下错误: ValueErro
从 0.4.0 版本开始,可以使用 torch.tensor 和 torch.Tensor 有什么区别?提供这两个非常相似且令人困惑的替代方案的原因是什么? 最佳答案 在 PyTorch 中,torc
PyTorch0.4中,.data 仍保留,但建议使用 .detach(), 区别在于 .data 返回和 x 的相同数据 tensor, 但不会加入到x的计算历史里,且require s_grad
我有一个参差不齐的张量,在尝试创建模型并使用 model.fit() 时,出现错误:TypeError: Failed to convert object of type to Tensor. Co
我必须用生成器和判别器训练一个 GAN 网络。我的发电机网络如下。 def Generator(image_shape=(512,512,3): inputs = Input(image_shap
我正在使用 Flask 运行 Web 服务器,当我尝试使用 vgg16 时出现错误,vgg16 是 keras 的预训练 VGG16 模型的全局变量。我不知道为什么会出现这个错误,也不知道它是否与 T
我正在使用 keras 的预训练模型,并且在调用 ResNet50(weights='imagenet') 时出现错误。 我在 flask 服务器中有以下代码: def getVGG16Predict
执行以下代码时出现以下错误。 rnn.rnn() 返回张量列表。错误在 convert_to_tensor 行。 TypeError: List of Tensors when single Tens
我有一个fruit_train_net.py 文件,其中包含以下代码 import tensorflow as tf import numpy as np import time import os
我们可以使用 torch.Tensor([1., 2.], device='cuda') 在 GPU 上分配张量.使用这种方式而不是torch.cuda.Tensor([1., 2.])有什么不同吗?
我是一名优秀的程序员,十分优秀!