我previously asked如何获取Tensor中的指针。我现在想弄清楚存储的数据类型,然后能够将namespace howOldAreYou
{
class Program
{
public static void Main(string[] args)
{
//variables
int birthYear;
string name;
System.DateTime moment = new System.DateTime();
int year = moment.Year;
//int month = moment.Month;
//int day = moment.Day;
//int minute = moment.Minute;
//int second = moment.Second;
//int millisecond = moment.Millisecond;
//Find out person's name
Console.WriteLine("What is your name?");
name = Console.ReadLine();
//Find what year person was born
Console.WriteLine("What year were you born, " + name + "?" );
if (int.TryParse(Console.ReadLine(), out birthYear))
{
Console.WriteLine(birthYear + "! Well you must be " + (year - birthYear));
}
else
{
Console.WriteLine("Sorry that's an invalid year");
}
}
}
}
转换为此数据类型。
Tensor有一个功能,
void*
DataType dtype() const { return shape_.data_type(); }
是一个简单的枚举,实际上并没有帮助我演员。我希望DataType
能够static_cast<type>
得到dtype()
的结果。所以我正在寻找一个可以为我做这个的宏或模板。
这样的存在吗?
答案 0 :(得分:0)
框架内置types.h
,用于queue_base.cc
不幸的是,因为它是一个模板,你需要使用宏等,以便每个不同的模板都被编译。
首先将类型作为模板参数的写入函数
template <DataType DT>
Status HandleSliceToElement(const Tensor& parent, Tensor* element,
int64 index) {
typedef typename EnumToDataType<DT>::Type T;
...
}
然后使用宏来处理每种数据类型。修剪简洁(见完整list of macros)
Status QueueBase::CopySliceToElement(const Tensor& parent, Tensor* element,
int64 index) {
#define HANDLE_TYPE(DT) \
if (parent.dtype() == DT) { \
TF_RETURN_IF_ERROR(HandleSliceToElement<DT>(parent, element, index)); \
return Status::OK(); \
}
HANDLE_TYPE(DT_FLOAT);
HANDLE_TYPE(DT_HALF);
HANDLE_TYPE(DT_DOUBLE);
...
#undef HANDLE_TYPE
return errors::Unimplemented("CopySliceToElement Unhandled data type: ",
parent.dtype());
}