在我的训练数据中,我有一个Unix时间戳,我想将其转换为hourOfDay和工作日。从那以后我想预测用户活动。我使用BigQuery在input_fn中使用numpy数组获取数据和一些计算。我想我需要运行input_fn的会话将我的Tensors转换为ndarrays。预测工作,模型训练好。无论如何这里是代码和错误消息:
def main():
export_dir = os.path.join(ROOT_DIR, MODEL_NAME)
def input_fn_from_bigquery():
features_dict = dict(
detectedActivity=tf.FixedLenFeature([1], tf.int64),
time=tf.FixedLenFeature([1], dtype=tf.int64),
)
training_dict = dict(
hourOfDay=tf.FixedLenFeature([1], tf.int64,default_value=0),
weekday=tf.FixedLenFeature([1], tf.int64, default_value=0),
time=tf.FixedLenFeature([1], dtype=tf.int64)
)
label_dict = dict(
detectedActivity=tf.FixedLenFeature([1], tf.int64)
)
# Create a Reader.
reader = bigquery_reader_ops.BigQueryReader(project_id=PROJECT,
dataset_id=DATASET,
table_id=TABLE,
timestamp_millis=TIME,
num_partitions=NUM_PARTITIONS,
features=features_dict)
queue = tf.train.string_input_producer(reader.partitions())
row_id, examples_serialized = reader.read_up_to(queue, 50)
features = tf.parse_example(examples_serialized, features=training_dict)
labels = tf.parse_example(examples_serialized, features=label_dict)
#get all times as numpy array and divide to correct timeformat
timearray = (tf.scalar_mul(0.001, tf.cast(features["time"], tf.float32))).eval()
hourarray = np.zeros(timearray.shape, dtype=np.int64)
weekdayarray = np.zeros(timearray.shape, dtype=np.int64)
for i in range(timearray.shape[0]):
hourarray[i] = datetime.fromtimestamp(timearray[i]).hour # calculate hours
weekdayarray[i] = datetime.fromtimestamp(timearray[i]).weekday() # calculate weekday
features["hourOfDay"] = tf.convert_to_tensor(hourarray, np.int64)
features["weekday"] = tf.convert_to_tensor(weekdayarray, np.int64)
return features, labels["detectedActivity"]
feature_columns = [tf.feature_column.numeric_column("weekday", shape=[1]),
tf.feature_column.numeric_column("hourOfDay", shape=[1])]
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10,10],
n_classes=5,
model_dir=export_dir)
print("Train")
classifier.fit(input_fn=input_fn_from_bigquery,max_steps=10)
def serving_input_fn():
feature_spec = {"weekday": tf.placeholder(dtype=tf.int64, shape=[1]),
"hourOfDay": tf.placeholder(dtype=tf.int64, shape=[1])}
return tf.contrib.learn.InputFnOps(features=feature_spec, labels=None, default_inputs=feature_spec)
print("Save model")
dir = classifier.export_savedmodel(export_dir_base=export_dir, serving_input_fn=serving_input_fn)
print(dir)
错误讯息:
ValueError:无法使用
eval()
评估张量:未注册默认会话。使用with sess.as_default()
或将显式会话传递给eval(session=sess)
如何将显式会话作为eval(session =?)参数传递。 期待有关此+的任何帮助,如果有其他方法从时间戳获得工作日和小时。我发现我的解决方案相当粗糙。