简体   繁体   English

TensorFlow:如何将tf.Tensor字符串转换为python datetime数据类型?

[英]TensorFlow: How convert tf.Tensor string to python datetime datatype?

In TensorFlow, trying to split date value read from file which is converted to Tensor string type by dataset api. 在TensorFlow中,尝试拆分从文件读取的日期值,该日期值已通过数据集api转换为Tensor字符串类型。 I want to convert this Tensor string datatype to python datetime type so that I can find day of week, hour, day, etc.. 我想将此Tensor字符串数据类型转换为python datetime类型,以便可以找到星期,小时,天等。

This is a very minimal example. 这是一个非常小的例子。 You could refactor it for your own purposes. 您可以出于自己的目的重构它。

import tensorflow as tf
from datetime import datetime


sess = tf.Session()

#Should be shortened
def convert_to_date(text):
    date = datetime.strptime(text.decode('ascii'), '%b %d %Y %I:%M%p')
    return date.strftime('%b %d %Y %I:%M%p') 

filenames = ["C:/Machine Learning/text.txt"]

dataset = tf.data.Dataset.from_tensor_slices(filenames)

tf.data.TextLineDataset
dataset = dataset.flat_map(
                   lambda filename :
                   tf.data.TextLineDataset( filename ) ).map( lambda text :
                                                                        tf.py_func(convert_to_date,
                                                                                  [text],
                                                                                  [tf.string]))
iterator = dataset.make_one_shot_iterator()
date = iterator.get_next()


print(sess.run([date]))

Output is 输出是

[(b'Jun 01 2005 01:33PM',)] [(b'Jun 01 2005 2005:01:33PM,)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TensorFlow:将 tf.Dataset 转换为 tf.Tensor - TensorFlow: convert tf.Dataset to tf.Tensor 将 tf.tensor 转换为 numpy - convert tf.tensor to numpy Tensorflow 类型错误:不允许将 `tf.Tensor` 用作 Python `bool`。 - Tensorflow Typeerror: Using a `tf.Tensor` as a Python `bool` is not allowed. TensorFlow:TypeError:不允许使用`tf.Tensor`作为Python`bool` - TensorFlow: TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed 如何在TensorFlow中清除tf.Tensor的形状信息? - How do I clear the shape information of tf.Tensor in TensorFlow? How to implement a numpy equation in the call of a tensorflow layer for a tensorflow model(无法将符号 tf.Tensor 转换为 numpy 数组) - How to implement a numpy equation in the call of a tensorflow layer for a tensorflow model (Cannot convert a symbolic tf.Tensor to a numpy array) Tensorflow:TypeError:预期的二进制或Unicode字符串,得到了 <tf.Tensor 'Placeholder:0' shape=<unknown> dtype = string&gt; - Tensorflow: TypeError: Expected binary or unicode string, got <tf.Tensor 'Placeholder:0' shape=<unknown> dtype=string> 如何在 tf.Tensor 中重塑和填充零 - How to reshape and padding zeros in tf.Tensor Tensorflow Reshape 失败并出现 TypeError:使用 `tf.Tensor` 作为 Python `bool` is not allowed - Tensorflow Reshape fails with TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed 如何将 map function 应用于 tf.Tensor - how to apply map function to the tf.Tensor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM