我正在尝试从张量字符串"2018/12/31 22:59"
中提取年,月,日,小时和分钟的值。我为此任务找到了此功能tf.string_split
,但是我的代码抛出了错误
Traceback (most recent call last):
File "path/to/my/file.py", line 12, in <module>
date = split_date_time[0]
TypeError: 'SparseTensor' object does not support indexing
这是代码
import tensorflow as tf
date_time = tf.placeholder(dtype=tf.string)
day = tf.placeholder(shape=[None], dtype=tf.int32),
month = tf.placeholder(shape=[None], dtype=tf.int32),
year = tf.placeholder(shape=[None], dtype=tf.int32),
hour = tf.placeholder(shape=[None], dtype=tf.int32),
minute = tf.placeholder(shape=[None], dtype=tf.int32)
split_date_time = tf.string_split(date_time, ' ')
date = split_date_time[0]
time = split_date_time[1]
date_splitted = tf.string_split(date, '-')
year = date_splitted[0]
month = date_splitted[1]
day = date_splitted[2]
time_spplitted = tf.string_split(time, ':')
hour = time_spplitted[0]
minute = time_spplitted[1]
with tf.Session() as sess:
print (sess.run(year, feed_dict={date_time: "2018-12-31 22:59"}))
print (sess.run(month, feed_dict={date_time: "2018-12-31 22:59"}))
print (sess.run(day, feed_dict={date_time: "2018-12-31 22:59"}))
print (sess.run(hour, feed_dict={date_time: "2018-12-31 22:59"}))
print (sess.run(minute, feed_dict={date_time: "2018-12-31 22:59"}))
答案 0 :(得分:1)
您的代码中有几个问题(主要是因为您显然没有阅读任何有关要使用的功能的文档)。我只会提到与您要解决的特定问题相关的一些关键问题(但我强烈建议您研究TensorFlow的基础知识及其计算模型)。
首先,如documentation of tf.string_split
所述,<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if (empty($first_name)) {
$nameErr = "First name is required";
} else {
$first_name = check_input($first_name);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$error = "Only letters and white space allowed";
} else {
$pass += 1;
}
}
if (empty($last_name)) {
$nameErr = "Last name is required";
} else {
$last_name = check_input($last_name);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$error = "Only letters and white space allowed";
}else {
$pass += 1;
}
}
if (empty($email)) {
$emailErr = "Email is required";
} else {
$email = check_input($email);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Invalid email format";
}else {
$pass += 1;
}
}
if (empty($phone)) {
$phoneErr = "Phone number is required";
} else {
$phone = check_input($phone);
if (!preg_match("'^(([\+]([\d]{2,}))([0-9\.\-\/\s]{5,})|([0-9\.\-\/\s]{5,}))*$'",$phone)) {
$error = "Invalid Phone Number";
}else {
$pass += 1;
}
}
if (empty($message)) {
$error = "Message cannot be blank";
} else {
$message = check_input($message);
$pass += 1;
}
}
$email_from =' Client, llc';
$email_subject = 'New Message From A Guest';
$email_body = "Name: $first_name $last_name\n".
"Email: $email\n".
"Phone: $phone\n".
"Message: $message.\n";
$to ="me@work.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (!isset($error) && $pass == '5'){
mail($to,$email_subject,$email_body,$headers);
header("location: thanks.html");
} else {
echo $error;
}
// purge the session after it is displayed
unset($error);
function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
的第一个参数应为“ 1-D字符串张量,要拆分的字符串”。但是,tf.string_split
是一个0-D字符串张量。
第二,"2018-12-31 22:59"
返回一个tf.SparseTensor
,该索引无法建立索引!
以下是您可能遇到的问题的解决方案:
tf.string_split