Python 利用张量流模型的预测
我使用以下代码创建了对新数据的预测:Python 利用张量流模型的预测,python,tensorflow,Python,Tensorflow,我使用以下代码创建了对新数据的预测: def predict(dfeval, importedModel): colNames = dfeval.columns dtypes = dfeval.dtypes predictions = [] for row in dfeval.iterrows(): example = tf.train.Example() for i in range(len(colNames)):
def predict(dfeval, importedModel):
colNames = dfeval.columns
dtypes = dfeval.dtypes
predictions = []
for row in dfeval.iterrows():
example = tf.train.Example()
for i in range(len(colNames)):
dtype = dtypes[i]
colName = colNames[i]
value = row[1][colName]
if dtype == "object":
value = bytes(value, "utf-8")
example.features.feature[colName].bytes_list.value.extend(
[value])
elif dtype == "float":
example.features.feature[colName].float_list.value.extend(
[value])
elif dtype == "int":
example.features.feature[colName].int64_list.value.extend(
[value])
predictions.append(
importedModel.signatures["predict"](
examples=tf.constant([example.SerializeToString()])))
return predictions
val = predict(dfeval, imported)
val
其中规定:
[{'predictions':}]
然后我可以通过以下方式打印值:
tf.print(val)
[{'predictions':[[0.249046683]]}]
但我希望在未来的计算中使用该值,例如:
val + 300
我希望得到回报:
300.249046683
但到目前为止,我还没有找到提取和使用预测的方法。你可以这样得到它:
val[0]['predictions'][0][0]
Val不仅仅是您要寻找的值,它是一个包含字典的列表,其中包含一个列表列表,列表列表中包含您的值,因此您只需从valYes中提取预测值,我明白了。我知道我不会做300+val,而是actual\u val=从val中获取值所需的一切300+actual\u val
。我的问题是,我需要做什么才能将actual_val
作为一个浮点,而不是一个张量。这允许我对张量值进行数学计算,但它仍然是一个张量,因此当我将值添加到数据集中时,单元格的内容显示为:tf.tensor(300.24905,shape=(),dtype=float32)
您可以尝试使用eval函数从tensor中获取值。我对tensor flow不是很有经验,但是您是否尝试过float(val[0]['predictions'][0][0])
?或.result()
或.numpy()
?val[0]['predictions']0][0]。numpy()
。非常感谢。