如何将Tensorflow预测保存到文件中? (首选CSV)

时间:2019-12-11 20:04:28

标签: python numpy tensorflow machine-learning keras

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()

这是我的代码。我已经训练了一个2011-2018年住房价格数据模型,并试图预测2019年住房价格。我想将预测结果保存到.csv文件中。 我的目标是创建一个简单的网页并使用Google Maps API显示预测,因此我需要每个单独的结果。但是,将预测结果保存到.csv文件的资源不多。

我该怎么做? 要么 有什么更好的方法可以实现我的目标?

1 个答案:

答案 0 :(得分:1)

这里是一个示例,其中我构建了一个简单的模型并进行了预测。我正在使用csv将预测另存为np.savetxt文件。您可以从here下载程序中正在使用的数据集。

代码-

%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=",")

输出-

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%

您会发现score.csv是在文件夹中创建的。

enter image description here

希望这能回答您的问题。学习愉快。