应遵循的一般模式是(见 这个文件 ):
input_fn
tf.data.Dataset
DnnRegressor
tf.Placeholder
None
tf.estimator.export.ServingInputReceiver
您的具体情况需要一些额外的细节。首先,您已将批量大小1硬编码到占位符中,相应的代码继续该假设。你的占位符必须有 shape=[None] 。
shape=[None]
不幸的是,你的代码是在假设形状的基础上编写的 1 ,例如, split_date_time.values[0] 将不再有效。我在下面的代码中添加了一个辅助函数来解决这个问题。
split_date_time.values[0]
这里有一些代码可能适合你:
import tensorflow as tf # tf.string_split returns a SparseTensor. When using a variable batch size, # this can be difficult to further manipulate. In our case, we don't need # a SparseTensor, because we have a fixed number of elements each split. # So we do the split and convert the SparseTensor to a dense tensor. def fixed_split(batched_string_tensor, delimiter, num_cols): # When splitting a batch of elements, the values array is row-major, e.g. # ["2018-01-02", "2019-03-04"] becomes ["2018", "01", "02", "2019", "03", "04"]. # So we simply split the string then reshape the array to create a dense # matrix with the same rows as the input, but split into columns, e.g., # [["2018", "01", "02"], ["2019", "03", "04"]] split = tf.string_split(batched_string_tensor, delimiter) return tf.reshape(split.values, [-1, num_cols]) def parse_dates(dates): split_date_time = fixed_split(dates, ' ', 2) date = split_date_time[:, 0] time = split_date_time[:, 1] # The values of the resulting SparseTensor will alternate between year, month, and day split_date = fixed_split(date, '-', 3) split_time = fixed_split(time, ':', 2) year = split_date[:, 0] month = split_date[:, 1] day = split_date[:, 2] hours = split_time[:, 0] minutes = split_time[:, 1] year = tf.string_to_number(year, out_type=tf.int32, name="year_temp") month = tf.string_to_number(month, out_type=tf.int32, name="month_temp") day = tf.string_to_number(day, out_type=tf.int32, name="day_temp") hours = tf.string_to_number(hours, out_type=tf.int32, name="hour_temp") minutes = tf.string_to_number(minutes, out_type=tf.int32, name="minute_temp") return {"year": year, "month": month, "day": day, "hours": hours, "minutes": minutes} def training_input_fn(): filenames = ["/var/data/file1.txt", "/var/data/file2.txt"] dataset = tf.data.TextLineDataset(filenames) dataset.batch(BATCH_SIZE) return parse_dates(iterator.get_next()) def serving_input_fn(): date_strings = tf.placeholder(dtype=tf.string, shape=[None], name="date_strings") features = parse_dates(date_strings) return tf.estimator.export.ServingInputReceiver(features, date_strings) with tf.Session() as sess: date_time_list = ["2018-12-31 22:59", "2018-01-23 2:09"] date_strings = tf.placeholder(dtype=tf.string, shape=[None], name="date_strings") features = parse_dates(date_strings) fetches = [features[k] for k in ["year", "month", "day", "hours", "minutes"]] year, month, day, hours, minutes = sess.run(fetches, feed_dict={date_strings: date_time_list}) print("Year =", year) print("Month =", month) print("Day =", day) print("Hours =", hours) print("Minutes =", minutes)