在PHP Laravel

时间:2017-10-23 15:37:15

标签: php python laravel tensorflow

我试图在Laravel中运行使用tensorflow库(不确定它是否相关)的python脚本。我的问题是在用TF库完成任何事情之后将任何消息返回给PHP。

我尝试过执行方法

$command = 'python C:/wamp64/www/hi.py';
$execMethod = exec($command);
$systemMethod = system($command);
$shellMethod = shell_exec($command);

我的python脚本:

#!c:/Program Files/Python36/python.exe

import os
import urllib.request
import tensorflow as tf
import numpy as np
import time

IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"

IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"


def main():
    # If the training and test sets aren't stored locally, download them.
    if not os.path.exists(IRIS_TRAINING):
        raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
        with open(IRIS_TRAINING, "wb") as f:
            f.write(raw)

    if not os.path.exists(IRIS_TEST):
        raw = urllib.request.urlopen(IRIS_TEST_URL).read()
        with open(IRIS_TEST, "wb") as f:
            f.write(raw)

    # Load datasets.
    training_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=IRIS_TRAINING, target_dtype=np.int,
                                                                       features_dtype=np.float32)
    test_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=IRIS_TEST, target_dtype=np.int,
                                                                   features_dtype=np.float32)



    # Specify that all features have real-value data


    feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]

    # Build 3 layer DNN with 10, 20, 10 units respectively.
    classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                            hidden_units=[10, 20, 10],
                                            n_classes=3,
                                            model_dir="c:/wamp64/www/Laravel/resources/pythonscripts/tmp/iris_model")  # Define the training inputs
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": np.array(training_set.data)},
        y=np.array(training_set.target),
        num_epochs=None,
        shuffle=True)

    # Train model.
    classifier.train(input_fn=train_input_fn, steps=1000)

    # Define the test inputs
    test_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": np.array(test_set.data)},
        y=np.array(test_set.target),
        num_epochs=1,
        shuffle=False)

    # Evaluate accuracy.
    accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]
    print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

在所有打印件为空之后,我在tf.contrib.learn.datasets.base.load_csv_with_header()之前获得所有输出。

我猜测我的脚本已完全执行,因为生成输出需要的时间与我在apache2 上运行所需的时间相同。 我按localhost/test.py

运行它

从php运行时,代码会生成文件并按预期保存分类器。输出就是问题。

我将欣赏任何分享的知识!

1 个答案:

答案 0 :(得分:0)

问题是,输出字符串包含\n,产生了空结果。我在返回json

时实现了我的结果
import json
output = []
output.append(item)
print(json.dumps({i : val for (i, val) in enumerate(output)}))