如何将Tensorflow预测保存到文件中?(首选CSV) [英] How Do I Save Tensorflow Predictions To A File? (CSV Preferred)
问题描述
将pandas导入为pd以md形式导入handler_data从数学导入sqrt从numpy导入串联从matplotlib导入pyplot从sklearn.preprocessing导入MinMaxScaler从sklearn.preprocessing导入LabelEncoder从sklearn.metrics导入mean_squared_error从keras.models导入顺序从keras.layers导入密集从keras.layers导入LSTM导入日期时间为dt将tensorflow导入为tf导入matplotlib.pyplot作为pltdf = pd.read_csv('중계동_dummies.csv')#火车参数seq_length = 365data_dim = 8hidden_dim = 10output_dim = 1learning_rate = 0.032迭代次数= 500trainX,trainY,testX,testY = md.preprocess_data(df,seq_length)X = tf.placeholder(tf.float32,[None,seq_length,data_dim])Y = tf.placeholder(tf.float32,[None,1])#建立一个LSTM网络cell = tf.contrib.rnn.BasicLSTMCell(num_units = hidden_dim,state_is_tuple = True,激活= tf.nn.relu)输出,_states = tf.nn.dynamic_rnn(cell,X,dtype = tf.float32)Y_pred = tf.contrib.layers.fully_connected(输出[:,-1],output_dim,activation_fn = None)#我们使用最后一个单元格的输出#成本/损失loss = tf.reduce_sum(tf.square(Y_pred-Y))#平方和#优化器优化器= tf.train.AdamOptimizer(learning_rate)火车= optimizer.minimize(损失)#RMSE目标= tf.placeholder(tf.float32,[无,1])预测= tf.placeholder(tf.float32,[无,1])rmse = tf.sqrt(tf.reduce_mean(tf.square(目标-预测)))mape = tf.reduce_mean(tf.abs(tf.divide(tf.subtract(预测,目标),(目标+ 1e-10)))))与tf.Session()作为sess:初始化= tf.global_variables_initializer()sess.run(初始化)saver = tf.train.Saver()#训练步骤对于我在范围内(迭代):_,step_loss = sess.run([train,loss],feed_dict = {X:trainX,Y:火车})print("[step:{}] loss:{}".format(i,step_loss))saver.save(sess,'my_test_model')#测试步骤test_predict = sess.run(Y_pred,feed_dict = {X:testX})test_predict = np.asarray(test_predict)np.savetxt("prediction.csv",test_predict,delimiter =,")rmse_val = sess.run(rmse,feed_dict = {目标:testY,预测:test_predict})mape_val = sess.run(mape,feed_dict = {目标:testY,预测:test_predict})print("RMSE:{},MAPE:{}".format(rmse_val,mape_val))#绘制预测plt.plot(testY)plt.plot(test_predict)plt.xlabel(时间段")plt.ylabel(公寓价格")plt.show()
这是我的代码.我已经针对2011-2018年的房价数据训练了一个模型,并试图预测2019年的房价.我想将预测结果保存到.csv文件中.我的目标是创建一个简单的网页并使用Google Maps API显示预测,因此我需要每个单独的结果.但是,用于将预测结果保存到.csv文件的资源并不多.
我该怎么做?或者有什么更好的方法可以实现我的目标?
在此示例中,我构建了一个简单的模型并进行了预测.我正在使用 np.savetxt
将预测另存为 csv
文件.您可以从
希望这能回答您的问题.学习愉快.
import pandas as pd
import manipulate_data as md
from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
import datetime as dt
import tensorflow as tf
import matplotlib.pyplot as plt
df = pd.read_csv('중계동_dummies.csv')
# train Parameters
seq_length = 365
data_dim = 8
hidden_dim = 10
output_dim = 1
learning_rate = 0.032
iterations = 500
trainX, trainY, testX, testY = md.preprocess_data(df,seq_length)
X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 1])
# build a LSTM network
cell = tf.contrib.rnn.BasicLSTMCell(
num_units=hidden_dim, state_is_tuple=True, activation=tf.nn.relu)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(
outputs[:, -1], output_dim, activation_fn=None) # We use the last cell's output
# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)
# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))
mape = tf.reduce_mean(tf.abs(tf.divide(tf.subtract(predictions,targets),(targets + 1e-10))))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver()
# Training step
for i in range(iterations):
_, step_loss = sess.run([train, loss], feed_dict={
X: trainX,
Y: trainY
})
print("[step: {}] loss: {}".format(i, step_loss))
saver.save(sess, 'my_test_model')
# Test step
test_predict = sess.run(Y_pred, feed_dict={X: testX})
test_predict = np.asarray(test_predict)
np.savetxt("prediction.csv", test_predict, delimiter=",")
rmse_val = sess.run(rmse, feed_dict={
targets: testY, predictions: test_predict})
mape_val = sess.run(mape, feed_dict={
targets: testY, predictions: test_predict})
print("RMSE: {}, MAPE: {}".format(rmse_val, mape_val))
# Plot predictions
plt.plot(testY)
plt.plot(test_predict)
plt.xlabel("Time Period")
plt.ylabel("Apartment Price")
plt.show()
This is my code. I have trained a model on housing price data from 2011-2018 and have attempted to predict housing prices for 2019. I want to save my prediction results to a .csv file. My goal is to create a simple webpage and display the predictions using Google Maps API, so I would need each individual result. However, not much resources exist for saving prediction results to a .csv file.
How can I do so? Or What are some better ways to achieve my goal?
Here is an example where I have built a simple model and doing the prediction. I am saving the prediction as csv
file using np.savetxt
. You can download the dataset I am using in the program from here.
Code -
%tensorflow_version 1.x
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# evaluate the model
score = model.predict(X,verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
# Save as csv
np.savetxt("score.csv", score, delimiter=",")
Output -
1.15.2
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/nn_impl.py:183: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 12) 108
_________________________________________________________________
dense_1 (Dense) (None, 8) 104
_________________________________________________________________
dense_2 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
acc: 74.48%
acc: 6.97%
You can find that the score.csv
is created in the folder.
Hope this answers your question. Happy Learning.
这篇关于如何将Tensorflow预测保存到文件中?(首选CSV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!