我目前正在学习LSTM.我从一本书中找到了一个代码来预测sin和cos混合曲线.但是,我坚持其推理功能.
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
import os
LOG_DIR = os.path.join(os.path.dirname(__file__), "log")
if os.path.exists(LOG_DIR) is False:
os.mkdir(LOG_DIR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def inference(x, n_batch, maxlen=None, n_hidden=None, n_out=None):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.zeros(shape, dtype=tf.float32)
return tf.Variable(initial)
cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)
initial_state = cell.zero_state(n_batch, tf.float32)
state = initial_state
outputs = []
with tf.variable_scope('LSTM'):
for t in range(maxlen):
if t > 0: …
Run Code Online (Sandbox Code Playgroud) 我是 Pytorch 的初学者。我试图编写参考 Pytorch 教程的 CNN 代码。下面是代码的一部分,但它显示错误“RuntimeError:变量数据必须是张量,但得到了列表”。我试图将输入数据转换为张量,但效果不佳。如果有人知道解决方案,请帮助我...
def read_labels(file):
dic = {}
with open(file) as f:
reader = f
for row in reader:
dic[row.split(",")[0]] = row.split(",")[1].rstrip() #rstrip(): eliminate "\n"
return dic
image_names= os.listdir("./train_mini")
label_dic = read_labels("labels.csv")
names =[]
labels = []
images =[]
for name in image_names[1:]:
images.append(cv2.imread("./train_mini/"+name))
labels.append(label_dic[os.path.splitext(name)[0]])
"""
Data distribution
"""
N = len(images)
N_train = int(N * 0.7)
N_test = int(N*0.2)
X_train, X_tmp, Y_train, Y_tmp = train_test_split(images, labels, train_size=N_train)
X_validation, X_test, Y_validation, Y_test = train_test_split(X_tmp, Y_tmp, test_size=N_test) …
Run Code Online (Sandbox Code Playgroud)