用于回归的TensorFlow dynamic_rnn输入(TensorFlow dynamic_rnn input for regression)
我试图将现有的张量流序列转换为序列分类器到回归器。
目前我一直在处理
tf.nn.dynamic_rnn()
的输入。 根据文档和其他答案,输入应该是(batch_size, sequence_length, input_size)
的形状。 但是我的输入数据只有两个维度:(sequence_length, batch_size)
。在将输入提供给
dynamic_rnn()
之前,原始解决方案使用tf.nn.embedding_lookup()
作为中间步骤。 如果我理解正确,我相信我不需要这一步,因为我正在处理回归问题,而不是分类问题。我需要embedding_lookup步骤吗? 如果是这样,为什么? 如果没有,我如何将我的
encoder_inputs
直接装入dynamic_rnn()
?以下是一般工作的最小化示例:
import numpy as np import tensorflow as tf tf.reset_default_graph() sess = tf.InteractiveSession() PAD = 0 EOS = 1 VOCAB_SIZE = 10 # Don't think I should need this for regression? input_embedding_size = 20 encoder_hidden_units = 20 decoder_hidden_units = encoder_hidden_units LENGTH_MIN = 3 LENGTH_MAX = 8 VOCAB_LOWER = 2 VOCAB_UPPER = VOCAB_SIZE BATCH_SIZE = 10 def get_random_sequences(): sequences = [] for j in range(BATCH_SIZE): random_numbers = np.random.randint(3, 10, size=8) sequences.append(random_numbers) sequences = np.asarray(sequences).T return(sequences) def next_feed(): batch = get_random_sequences() encoder_inputs_ = batch eos = np.ones(BATCH_SIZE) decoder_targets_ = np.hstack((batch.T, np.atleast_2d(eos).T)).T decoder_inputs_ = np.hstack((np.atleast_2d(eos).T, batch.T)).T #print(encoder_inputs_) #print(decoder_inputs_) return { encoder_inputs: encoder_inputs_, decoder_inputs: decoder_inputs_, decoder_targets: decoder_targets_, } ### "MAIN" # Placeholders encoder_inputs = tf.placeholder(shape=(LENGTH_MAX, BATCH_SIZE), dtype=tf.int32, name='encoder_inputs') decoder_targets = tf.placeholder(shape=(LENGTH_MAX + 1, BATCH_SIZE), dtype=tf.int32, name='decoder_targets') decoder_inputs = tf.placeholder(shape=(LENGTH_MAX + 1, BATCH_SIZE), dtype=tf.int32, name='decoder_inputs') # Don't think I should need this for regression problems embeddings = tf.Variable(tf.random_uniform([VOCAB_SIZE, input_embedding_size], -1.0, 1.0), dtype=tf.float32) encoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, encoder_inputs) decoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, decoder_inputs) # Encoder RNN encoder_cell = tf.contrib.rnn.LSTMCell(encoder_hidden_units) encoder_outputs, encoder_final_state = tf.nn.dynamic_rnn( encoder_cell, encoder_inputs_embedded, # Throws 'ValueError: Shape (8, 10) must have rank at least 3' if encoder_inputs is used dtype=tf.float32, time_major=True, ) # Decoder RNN decoder_cell = tf.contrib.rnn.LSTMCell(decoder_hidden_units) decoder_outputs, decoder_final_state = tf.nn.dynamic_rnn( decoder_cell, decoder_inputs_embedded, initial_state=encoder_final_state, dtype=tf.float32, time_major=True, scope="plain_decoder", ) decoder_logits = tf.contrib.layers.linear(decoder_outputs, VOCAB_SIZE) decoder_prediction = tf.argmax(decoder_logits, 2) # Loss function loss = tf.reduce_mean(tf.squared_difference(decoder_logits, tf.one_hot(decoder_targets, depth=VOCAB_SIZE, dtype=tf.float32))) train_op = tf.train.AdamOptimizer().minimize(loss) sess.run(tf.global_variables_initializer()) max_batches = 5000 batches_in_epoch = 500 print('Starting train') try: for batch in range(max_batches): feed = next_feed() _, l = sess.run([train_op, loss], feed) if batch == 0 or batch % batches_in_epoch == 0: print('batch {}'.format(batch)) print(' minibatch loss: {}'.format(sess.run(loss, feed))) predict_ = sess.run(decoder_prediction, feed) for i, (inp, pred) in enumerate(zip(feed[encoder_inputs].T, predict_.T)): print(' sample {}:'.format(i + 1)) print(' input > {}'.format(inp)) print(' predicted > {}'.format(pred)) if i >= 2: break print() except KeyboardInterrupt: print('training interrupted')
我已经在stackoverflow上阅读了类似的问题,但我发现自己仍然对如何解决这个问题感到困惑。
编辑:我想我应该澄清上面的代码运行良好,但真正的期望输出应该模仿一个有噪声的信号(例如文本到语音),这就是为什么我认为我需要连续的输出值而不是单词或字母。
I'm stuck trying to convert an existing tensorflow sequence to sequence classifier to a regressor.
Currently I'm stuck in handling the input for
tf.nn.dynamic_rnn()
. According to the documentation and other answers, input should be in the shape of(batch_size, sequence_length, input_size)
. However my input data has only two dimensions:(sequence_length, batch_size)
.The original solution uses
tf.nn.embedding_lookup()
as an intermediate step before feeding input todynamic_rnn()
. If I understand correctly, I believe I don't need this step since I'm working on a regression problem, not a classification problem.Do I need the embedding_lookup step? If so, why? If not, how can I fit my
encoder_inputs
directly intodynamic_rnn()
?Below is a working minimalized example of the general idea:
import numpy as np import tensorflow as tf tf.reset_default_graph() sess = tf.InteractiveSession() PAD = 0 EOS = 1 VOCAB_SIZE = 10 # Don't think I should need this for regression? input_embedding_size = 20 encoder_hidden_units = 20 decoder_hidden_units = encoder_hidden_units LENGTH_MIN = 3 LENGTH_MAX = 8 VOCAB_LOWER = 2 VOCAB_UPPER = VOCAB_SIZE BATCH_SIZE = 10 def get_random_sequences(): sequences = [] for j in range(BATCH_SIZE): random_numbers = np.random.randint(3, 10, size=8) sequences.append(random_numbers) sequences = np.asarray(sequences).T return(sequences) def next_feed(): batch = get_random_sequences() encoder_inputs_ = batch eos = np.ones(BATCH_SIZE) decoder_targets_ = np.hstack((batch.T, np.atleast_2d(eos).T)).T decoder_inputs_ = np.hstack((np.atleast_2d(eos).T, batch.T)).T #print(encoder_inputs_) #print(decoder_inputs_) return { encoder_inputs: encoder_inputs_, decoder_inputs: decoder_inputs_, decoder_targets: decoder_targets_, } ### "MAIN" # Placeholders encoder_inputs = tf.placeholder(shape=(LENGTH_MAX, BATCH_SIZE), dtype=tf.int32, name='encoder_inputs') decoder_targets = tf.placeholder(shape=(LENGTH_MAX + 1, BATCH_SIZE), dtype=tf.int32, name='decoder_targets') decoder_inputs = tf.placeholder(shape=(LENGTH_MAX + 1, BATCH_SIZE), dtype=tf.int32, name='decoder_inputs') # Don't think I should need this for regression problems embeddings = tf.Variable(tf.random_uniform([VOCAB_SIZE, input_embedding_size], -1.0, 1.0), dtype=tf.float32) encoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, encoder_inputs) decoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, decoder_inputs) # Encoder RNN encoder_cell = tf.contrib.rnn.LSTMCell(encoder_hidden_units) encoder_outputs, encoder_final_state = tf.nn.dynamic_rnn( encoder_cell, encoder_inputs_embedded, # Throws 'ValueError: Shape (8, 10) must have rank at least 3' if encoder_inputs is used dtype=tf.float32, time_major=True, ) # Decoder RNN decoder_cell = tf.contrib.rnn.LSTMCell(decoder_hidden_units) decoder_outputs, decoder_final_state = tf.nn.dynamic_rnn( decoder_cell, decoder_inputs_embedded, initial_state=encoder_final_state, dtype=tf.float32, time_major=True, scope="plain_decoder", ) decoder_logits = tf.contrib.layers.linear(decoder_outputs, VOCAB_SIZE) decoder_prediction = tf.argmax(decoder_logits, 2) # Loss function loss = tf.reduce_mean(tf.squared_difference(decoder_logits, tf.one_hot(decoder_targets, depth=VOCAB_SIZE, dtype=tf.float32))) train_op = tf.train.AdamOptimizer().minimize(loss) sess.run(tf.global_variables_initializer()) max_batches = 5000 batches_in_epoch = 500 print('Starting train') try: for batch in range(max_batches): feed = next_feed() _, l = sess.run([train_op, loss], feed) if batch == 0 or batch % batches_in_epoch == 0: print('batch {}'.format(batch)) print(' minibatch loss: {}'.format(sess.run(loss, feed))) predict_ = sess.run(decoder_prediction, feed) for i, (inp, pred) in enumerate(zip(feed[encoder_inputs].T, predict_.T)): print(' sample {}:'.format(i + 1)) print(' input > {}'.format(inp)) print(' predicted > {}'.format(pred)) if i >= 2: break print() except KeyboardInterrupt: print('training interrupted')
I have read similar questions here on stackoverflow but find my self still puzzled as to how to solve this.
EDIT: I think I should clarify that the code above works well, however the real desired output should mimic a noisy signal (text to speech for example) which is why I think I need continuous output values instead of words or letters.
原文:https://stackoverflow.com/questions/44871420
最满意答案
gtk.main()
运行直到关闭窗口(它调用“主循环”或“事件循环”并且它在GUI程序中执行所有操作 - 获取键/鼠标事件,将其发送到窗口小部件,重绘窗口小部件,在ypu按下时运行函数按钮等)。您必须使用
Threading
同时运行(长时间运行)代码或使用GUI中的某些Timer
类来定期执行某些代码。
gtk.main()
runs till you close window (it is call "main loop" or "event loop" and it does everything in GUI program - get key/mouse event, send it to widgets, redraw widgets, run functions when ypu press button, etc.).You have to use
Threading
to run (long-running) code at the same time or use someTimer
class in GUI to execute some code periodically.
相关问答
更多-
Python GTK3:如何创建Gtk.FileChooseDialog?(Python GTK3: how to create a Gtk.FileChooseDialog?)[2022-05-31]
只需按照说明操作并使用关键字参数即可。 我还将按钮更改为使用.add_buttons()因为它还抛出了DeprecationWarning: import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk dialog = Gtk.FileChooserDialog( title="Please choose a folder", action=Gtk.FileChooserAction.SELECT_FOL ... -
gtk功能 : gtk.main()函数运行主循环,直到gtk.main_quit()函数。 因此,您需要在单独的线程中运行while循环,并在完成后调用main_quit() 。 gtk Functions: The gtk.main() function runs the main loop until the gtk.main_quit() function is called. So you need to run your while loop in a separate thread and c ...
-
Python Gtk3使用Gtk.Container创建简单的容器对象(Python Gtk3 creating simple container object with Gtk.Container)[2022-05-27]
Gtk.Container不是Widget,它是您必须实现的接口。 这不太可能是你想要实际做的事情,因为实现一个新容器并非易事。 如果你想让它包含多个孩子,你想要使用的可能是Gtk.Box Gtk.Bin如果你只想要一个孩子,你可能想要使用Gtk.Box 。 Gtk.Container is not a Widget it is an Interface that you have to implement. It is unlikely that is what you want to actually ... -
Gtk3窗口背景图像(Gtk3 window background image)[2022-12-13]
检查以下代码是否适合您: #includeint main( int argc, char *argv[]) { GtkWidget *window; GtkWidget *layout; GtkWidget *image; GtkWidget *button; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window ... -
Gtk Python回调(Gtk Python Callback)[2022-07-02]
不是100%确定你想要做什么,但如果你想根据输入的文本过滤树视图显示的内容,你应该看一下GtkTreeModelFilter 。 您可以将它用作TreeModel并设置您自己的VisibleFunc,它根据输入的文本决定行是否可见。 当文本改变时,只需调用refilter():它将为每一行调用VisibleFunc()。 很抱歉没有为您提供python示例或文档,我希望这仍然有帮助...... Not 100% sure what you want to do but if you want to filt ... -
Python Gtk3右键菜单(Python Gtk3 right click menu)[2022-03-27]
由于menu是show_menu()函数中的一个局部变量,并且它没有被别的引用,所以它的引用计数会降到0,并在函数结束时被销毁。 不幸的是,当你期望看到它时,这是正确的。 相反,在全局范围内创建menu使得menu不再局限于一个函数,因此它不会在函数结束时被销毁。 from gi.repository import Gtk def show_menu(self, *args): i1 = Gtk.MenuItem("Item 1") menu.append(i1) i2 = Gtk ... -
gtk.main()运行直到关闭窗口(它调用“主循环”或“事件循环”并且它在GUI程序中执行所有操作 - 获取键/鼠标事件,将其发送到窗口小部件,重绘窗口小部件,在ypu按下时运行函数按钮等)。 您必须使用Threading同时运行(长时间运行)代码或使用GUI中的某些Timer类来定期执行某些代码。 gtk.main() runs till you close window (it is call "main loop" or "event loop" and it does everything in ...
-
您可以使用与GTK主循环集成的Gio.Subprocess ,而不是使用Python的subprocess Gio.Subprocess模块: #!/usr/bin/python3 from gi.repository import Gtk, Gio # ... class updateWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Updating...") s ...
-
Gtk日历填满整个窗口(Gtk Calendar fill whole window)[2022-02-23]
这显示了如何增加行高。 可悲的是,似乎没有row_expand()可以这么说。 我想你可以在调整大小时总是得到窗口的高度,并做一些数学来计算行的高度...... class MainWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self) self.calendar = Gtk.Calendar() self.calendar.set_detail_height_rows( ... -
您实际上没有启动该线程,您只实例化了一个可用于启动它的对象。 完整的解决方案需要仔细分离GUI线程和工作线程之间的职责。 你想要做的是以下内容: 在单独的线程中进行繁重的计算,由GUI代码生成并加入。 计算不应该生成自己的线程,也不需要知道线程(当然,除了线程安全之外)。 线程完成后,使用gobject.idle_add()告诉GUI可以撤消进度指示器。 ( gobject.idle_add是唯一可以安全地从另一个线程调用的GTK函数。) 通过这样的设置,无论计算如何,GUI都保持完全响应并且进度条更新,并 ...