这是交易,我得到一个python脚本,作为输入,我有2个数组,一个数组具有一个函数的值(x_train),另一个数组具有测量值的时间(y_train)。 该脚本是从Java调用的,其参数如下所示:
a)
[38.0, 45.0, 42.0, 35.0, 41.0, 45.0, 69.0, 35.0, 33.0, 34.0]
[85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0]
b)
[38.0, 45.0, 42.0, 35.0, 41.0, 45.0, 69.0, 35.0, 33.0, 34.0]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
c)
[38.0, 45.0, 42.0, 35.0, 41.0, 45.0, 69.0, 35.0, 33.0, 34.0]
[1.562571106116E12, 1.562657506116E12, 1.562743906116E12, 1.562830306116E12, 1.562916706116E12, 1.563003106116E12, 1.563089506116E12, 1.563175906116E12, 1.563262306116E12, 1.563348706116E12]
我需要做一个9级函数并预测数组的下一个元素,即函数的系数(a9,a8,a7,a6,a5,a4,a3,a2,a1, a0和损失),然后用它们计算第11个数字。
问题在于,当我使用选项(a)和(c)时,由于该过程,我得到所有系数的NAN,但是当我使用选项(b)时,我得到了一些结果(这是问题函数的启动参数,我将在稍后介绍),并使用这些参数进行工作。
该脚本不会引发任何错误,我是这个主题的新手,可以使用一些帮助。
这是整个python脚本:
import tensorflow
import sys
s1 = sys.stdin.readline().strip()
s2 = sys.stdin.readline().strip()
s3 = sys.stdin.readline().strip()
#Trainable Parameters
startParam0 = 2
startParam1 = 0.1
startParam2 = 0.1
startParam3 = 0.00016
startParam4 = 0.0000002
startParam5 = 0.0000000000238
startParam6 = 0.000000003202899
startParam7 = 0.000000000000041
startParam8 = 0.000000000000001
startParam9 = 0.0000000000000001
a0 = tensorflow.Variable([startParam0], dtype=tensorflow.float32)
a1 = tensorflow.Variable([startParam1], dtype=tensorflow.float32)
a2 = tensorflow.Variable([startParam2], dtype=tensorflow.float32)
a3 = tensorflow.Variable([startParam3], dtype=tensorflow.float32)
a4 = tensorflow.Variable([startParam4], dtype=tensorflow.float32)
a5 = tensorflow.Variable([startParam5], dtype=tensorflow.float32)
a6 = tensorflow.Variable([startParam6], dtype=tensorflow.float32)
a7 = tensorflow.Variable([startParam7], dtype=tensorflow.float32)
a8 = tensorflow.Variable([startParam8], dtype=tensorflow.float32)
a9 = tensorflow.Variable([startParam9], dtype=tensorflow.float32)
a10 = tensorflow.Variable([startParam2], dtype=tensorflow.float32)
a11 = tensorflow.Variable([startParam3], dtype=tensorflow.float32)
a12 = tensorflow.Variable([startParam4], dtype=tensorflow.float32)
a13 = tensorflow.Variable([startParam4], dtype=tensorflow.float32)
a14 = tensorflow.Variable([startParam3], dtype=tensorflow.float32)
a15 = tensorflow.Variable([startParam2], dtype=tensorflow.float32)
a16 = tensorflow.Variable([startParam1], dtype=tensorflow.float32)
a17 = tensorflow.Variable([startParam1], dtype=tensorflow.float32)
a18 = tensorflow.Variable([startParam2], dtype=tensorflow.float32)
a19 = tensorflow.Variable([startParam3], dtype=tensorflow.float32)
variableArray = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19]
curr_a0 = 0
curr_a1 = 0
curr_a2 = 0
curr_a3 = 0
curr_a4 = 0
curr_a5 = 0
curr_a6 = 0
curr_a7 = 0
curr_a8 = 0
curr_a9 = 0
curr_a10 = 0
curr_a11 = 0
curr_a12 = 0
curr_a13 = 0
curr_a14 = 0
curr_a15 = 0
curr_a16 = 0
curr_a17 = 0
curr_a18 = 0
curr_a19 = 0
curr_loss = 0
outputArray = [curr_a0, curr_a1, curr_a2, curr_a3, curr_a4, curr_a5, curr_a6, curr_a7, curr_a8, curr_a9, curr_a10, curr_a11, curr_a12, curr_a13, curr_a14, curr_a15, curr_a16, curr_a17, curr_a18, curr_a19]
#Training Data (inputs/outputs)
x = tensorflow.placeholder(dtype=tensorflow.float32)
y = tensorflow.placeholder(dtype=tensorflow.float32)
# recive parameters from java
lines = s1.split("[")[1].split("]")[0].split(",");
y_train = []
counter = 0
for l in lines:
s = l.strip()
y_train.append(float(l.strip()))
counter = counter + 1
lines = s2.split("[")[1].split("]")[0].split(",");
x_train = []
counter = 0
for l in lines:
s = l.strip()
x_train.append(float(s))
counter = counter +1
grade = int(s3.strip());
linear_model = tensorflow.Variable([0], dtype=tensorflow.float32)
sessionInput = [ None for k in range( grade + 2 ) ]
sessionOutput = [ None for k in range( grade + 2 ) ]
for i in range(grade + 1):
s = 1
for j in range (i):
s = s * x
linear_model = linear_model + variableArray[i] * s
sessionInput[i] = variableArray[grade - i]
sessionOutput[i] = outputArray[grade - i]
#Linear Regression Loss Function - sum of the squares
squared_deltas = tensorflow.square(linear_model - y_train)
loss = tensorflow.reduce_sum(squared_deltas)
curr_loss = 0
sessionInput[grade+1] = loss
sessionOutput[grade+1] = curr_loss
#Gradient descent optimizer
#optimizer = tensorflow.train.GradientDescentOptimizer(learning_rate=0.01)
optimizer = tensorflow.train.AdamOptimizer(learning_rate=0.25)
train = optimizer.minimize(loss=loss)
#Creating a session
sess = tensorflow.Session()
writer = tensorflow.summary.FileWriter("/tmp/log/", sess.graph)
#Initializing variables
init = tensorflow.global_variables_initializer()
sess.run(init)
#Optimizing the parameters
for i in range(10000):
sess.run(train, feed_dict={x: x_train, y: y_train})
#Print the parameters and loss
outputArray = sess.run(sessionInput, {x: x_train, y: y_train})
s = ""
for i in range(0, grade + 2):
s = s + str(outputArray[i])
if not(i == grade + 1):
s = s + ","
print(s)
writer.close()
sess.close()
和Java代码:
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
PrintWriter stdOutput = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
System.out.println(values); //y_train
stdOutput.println(values);
stdOutput.flush();
System.out.println(dates); //x_train
stdOutput.println(dates);
stdOutput.flush();
stdOutput.println(grade); //grade=9
stdOutput.flush();
// for reading the ouput from stream
String s1 = null;
List<Double> coefList = new ArrayList<Double>();
while ((s1 = stdInput.readLine()) != null) {
System.out.println(s1);
if (s1.indexOf("\\[") == -1) {
Integer n = s1.split("\\[").length;
if (n == grade + 2) {
try {
for(int i = 0; i < grade+1; i++){
coefList.add( Double.parseDouble(s1.split("\\[")[1 + i].split("\\]")[0]));
}
} catch(NumberFormatException E) {
System.err.println("Received NAN instead of a number for the parameters!");
}
}
}
}
最后,它会打印系数。
在选项(b)中,结果是这样的:
[4.3143527e-06],[-1.3367743e-05],[5.2168118e-05],[0.00026263],[0.0007228],[0.00161756],[0.00351851],[0.10692083],[0.11532306],[2.0412211],17321398.0
第11个数字的预测值是:58.70016770335398(似乎还可以)
在选项(a)和(c)中,结果是:[nan],[nan],[nan],[nan],[nan],[nan],[nan],[nan],[ nan],[nan],nan
没有错误!请帮我解决这个问题...因为我需要参数以毫秒为单位(如选项(c)所示)
让我知道您是否还需要其他任何内容。只要结果保持在参数范围内,就可以考虑使用任何选项,甚至可以降低功能等级或其他功能。
谢谢。