我是tf初学者,将demo改动一下就不行。python 3.6 +tf1.12 试过网上所有办法都不行。。一到语句: x_in = sess.run(x_data) 就报错:Process finished with exit code -1073741819 (0xC0000005)
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # or any {'0', '1', '2'}
import tensorflow as tf
import numpy as np
import logging
logging.basicConfig(level=logging.WARNING,
filename='./log.txt',
filemode='w',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# Global parameters
DATA_FILE = 'E://wf//python//600718//600718_cl.csv'
BATCH_SIZE = 1
NUM_FEATURES = 154
NUM = 30
np.set_printoptions(threshold=np.inf)
print("TensorFlow version: {}".format(tf.version.VERSION))
# 定义一个将文件名作为参数的函数,并返回大小等于 BATCH_SIZE 的张量
def data_generator(filename):
"""
Generates Tensors in batches of size Batch SIZE
Args:String Tensor
Filename from which data is to be read
Returns: Tensors
feature batch and label batch
"""
f_queue = tf.train.string_input_producer(filename)
reader = tf.TextLineReader(skip_header_lines=1)
key,value = reader.read(f_queue)
print('data_generator','value:',value)
record_defaults = [(0.0) for _ in range(NUM_FEATURES)]
data = tf.io.decode_csv(value, record_defaults=record_defaults)
features = tf.gather_nd(data, [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9],
[10], [11], [12], [13], [14], [15], [16], [17], [18], [19],
[20], [21], [22], [23], [24], [25], [26], [27], [28], [29],
[30], [31], [32], [33], [34], [35], [36], [37], [38], [39],
[40], [41], [42], [43], [44], [45], [46], [47], [48], [49],
[50], [51], [52], [53], [54], [55], [56], [57], [58], [59],
[60], [61], [62], [63], [64], [65], [66], [67], [68], [69],
[70], [71], [72], [73], [74], [75], [76], [77], [78], [79],
[80], [81], [82], [83], [84], [85], [86], [87], [88], [89],
[90], [91], [92], [93], [94], [95], [96], [97], [98], [99],
[100], [101], [102], [103], [104], [105], [106], [107], [108], [109],
[110], [111], [112], [113], [114], [115], [116], [117], [118], [119],
[120], [121], [122], [123], [124], [125], [126], [127], [128], [129],
[130], [131], [132], [133], [134], [135], [136], [137], [138], [139],
[140], [141], [142], [143], [144], [145], [146], [147], [148], [149]])
label = tf.gather_nd(data, [[150], [151], [152], [153]])#tf.stack()
return features, label
# 添加层
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random.normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
def train(dir):
# 1.训练的数据
x_data, y_data = data_generator([DATA_FILE])
print(y_data, 'hi')
# 2.定义节点准备接收数据
# define placeholder for inputs to network
xs = tf.compat.v1.placeholder(tf.float32, [None, 150])
ys = tf.compat.v1.placeholder(tf.float32, [None, 4])
# 3.定义神经层:隐藏层和预测层
# add hidden layer 输入值是 xs,在隐藏层有 10 个神经元 tf.nn.relu
l1 = add_layer(xs, 150, 1000, activation_function=None)
# add output layer 输入值是隐藏层 l1,在预测层输出 1 个结果
prediction = add_layer(l1, 1000, 4, activation_function=None)
# 4.定义 loss 表达式
# the error between prediciton and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
# 5.选择 optimizer 使 loss 达到最小
# 这一行定义了用什么方式去减少 loss,学习率是 0.1
train_step = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()#initialize_all_variables()
sess = tf.compat.v1.Session()
# 上面定义的都没有运算,直到 sess.run 才会开始运算
sess.run(init)
# 迭代 1000 次学习,sess.run optimizer
for i in range(1000):
# 创建一个线程协调器
coord = tf.train.Coordinator()
# 开启读文件的子线程 (和tf.train.string_input_producer配合使用)
threads = tf.train.start_queue_runners(sess, coord=coord)
# 打印读取的内容
print(sess.run([x_data, y_data]))
x_in = sess.run(x_data)
y_in = sess.run(y_data)
sess.run(train_step, feed_dict={xs: x_in, ys: y_in})
if i % 50 == 0:
# to see the step improvement
logging.debug(sess.run(loss, feed_dict={xs: x_in, ys: y_in}))
# 结束子线程
coord.request_stop()
# 等待子线程结束
coord.join(threads)
if __name__ == '__main__':
train('E:\wf\python\600718')