def _loop(function, itr):
     prev_h1, prev_h2, prev_h3 = [
         np_zeros((minibatch_size, n_hid)) for i in range(3)
     ]
     prev_kappa = np_zeros((minibatch_size, att_size))
     prev_w = np_zeros((minibatch_size, n_chars))
     X_mb, X_mb_mask, c_mb, c_mb_mask = next(itr)
     n_cuts = len(X_mb) // cut_len + 1
     partial_costs = []
     for n in range(n_cuts):
         start = n * cut_len
         stop = (n + 1) * cut_len
         if len(X_mb[start:stop]) < cut_len:
             new_len = cut_len - len(X_mb) % cut_len
             zeros = np.zeros((new_len, X_mb.shape[1], X_mb.shape[2]))
             zeros = zeros.astype(X_mb.dtype)
             mask_zeros = np.zeros((new_len, X_mb_mask.shape[1]))
             mask_zeros = mask_zeros.astype(X_mb_mask.dtype)
             X_mb = np.concatenate((X_mb, zeros), axis=0)
             X_mb_mask = np.concatenate((X_mb_mask, mask_zeros), axis=0)
             assert len(X_mb[start:stop]) == cut_len
             assert len(X_mb_mask[start:stop]) == cut_len
         rval = function(X_mb[start:stop], X_mb_mask[start:stop], c_mb,
                         c_mb_mask, prev_h1, prev_h2, prev_h3, prev_kappa,
                         prev_w)
         current_cost = rval[0]
         prev_h1, prev_h2, prev_h3 = rval[1:4]
         prev_h1 = prev_h1[-1]
         prev_h2 = prev_h2[-1]
         prev_h3 = prev_h3[-1]
         prev_kappa = rval[4][-1]
         prev_w = rval[5][-1]
     partial_costs.append(current_cost)
     return partial_costs
 def _loop(function, itr):
     prev_h1, prev_h2 = [np_zeros((minibatch_size, n_hid))
                                  for i in range(2)]
     prev_kappa = np_zeros((minibatch_size, att_size))
     prev_w = np_zeros((minibatch_size, n_chars))
     X_mb, X_mb_mask, c_mb, c_mb_mask = next(itr)
     n_cuts = len(X_mb) // cut_len + 1
     partial_costs = []
     for n in range(n_cuts):
         start = n * cut_len
         stop = (n + 1) * cut_len
         if len(X_mb[start:stop]) < cut_len:
             new_len = cut_len - len(X_mb) % cut_len
             zeros = np.zeros((new_len, X_mb.shape[1],
                               X_mb.shape[2]))
             zeros = zeros.astype(X_mb.dtype)
             mask_zeros = np.zeros((new_len, X_mb_mask.shape[1]))
             mask_zeros = mask_zeros.astype(X_mb_mask.dtype)
             X_mb = np.concatenate((X_mb, zeros), axis=0)
             X_mb_mask = np.concatenate((X_mb_mask, mask_zeros), axis=0)
             assert len(X_mb[start:stop]) == cut_len
             assert len(X_mb_mask[start:stop]) == cut_len
         rval = function(X_mb[start:stop],
                         X_mb_mask[start:stop],
                         c_mb, c_mb_mask,
                         prev_h1, prev_h2, prev_kappa, prev_w)
         current_cost = rval[0]
         prev_h1, prev_h2 = rval[1:3]
         prev_h1 = prev_h1[-1]
         prev_h2 = prev_h2[-1]
         prev_kappa = rval[3][-1]
         prev_w = rval[4][-1]
     partial_costs.append(current_cost)
     return partial_costs
Exemple #3
0
 def _loop(function, itr):
     prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                  for i in range(3)]
     X_mb, X_mb_mask, y_mb, y_mb_mask = next(itr)
     n_cuts = len(X_mb) // cut_len + 1
     partial_costs = []
     for n in range(n_cuts):
         start = n * cut_len
         stop = (n + 1) * cut_len
         if len(X_mb[start:stop]) < cut_len:
             # skip end edge case
             break
             new_len = cut_len - len(X_mb) % cut_len
             zeros = np.zeros((new_len, X_mb.shape[1],
                               X_mb.shape[2]))
             zeros = zeros.astype(X_mb.dtype)
             mask_zeros = np.zeros((new_len, X_mb_mask.shape[1]))
             mask_zeros = mask_zeros.astype(X_mb_mask.dtype)
             X_mb = np.concatenate((X_mb, zeros), axis=0)
             X_mb_mask = np.concatenate((X_mb_mask, mask_zeros), axis=0)
             assert len(X_mb[start:stop]) == cut_len
             assert len(X_mb_mask[start:stop]) == cut_len
         rval = function(X_mb[start:stop],
                         X_mb_mask[start:stop],
                         y_mb[start:stop], y_mb_mask[start:stop],
                         prev_h1, prev_h2, prev_h3)
         current_cost = rval[0]
         prev_h1, prev_h2, prev_h3 = rval[1:4]
         prev_h1 = prev_h1[-1]
         prev_h2 = prev_h2[-1]
         prev_h3 = prev_h3[-1]
     partial_costs.append(current_cost)
     return partial_costs
 def _loop(function, itr):
     prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                  for i in range(3)]
     X_mb, X_mb_mask = next(itr)
     # sanity check that masking code is OK
     assert X_mb_mask.min() > 1E-6
     n_cuts = len(X_mb) // cut_len + 1
     partial_costs = []
     for n in range(n_cuts):
         if n % 100 == 0:
             print("step %i" % n, end="")
         else:
             print(".", end="")
         start = n * cut_len
         stop = (n + 1) * cut_len
         if len(X_mb[start:stop]) < cut_len:
             # skip end edge case
             break
         rval = function(X_mb[start:stop],
                         X_mb_mask[start:stop],
                         prev_h1, prev_h2, prev_h3)
         current_cost = rval[0]
         prev_h1, prev_h2, prev_h3 = rval[1:4]
         prev_h1 = prev_h1[-1]
         prev_h2 = prev_h2[-1]
         prev_h3 = prev_h3[-1]
     partial_costs.append(current_cost)
     print("")
     return partial_costs
 def _loop(function, itr):
     prev_h1, prev_h2, prev_h3 = [
         np_zeros((minibatch_size, n_hid)) for i in range(3)
     ]
     X_mb, X_mb_mask = next(itr)
     # sanity check that masking code is OK
     assert X_mb_mask.min() > 1E-6
     n_cuts = len(X_mb) // cut_len + 1
     partial_costs = []
     for n in range(n_cuts):
         if n % 100 == 0:
             print("step %i" % n, end="")
         else:
             print(".", end="")
         start = n * cut_len
         stop = (n + 1) * cut_len
         if len(X_mb[start:stop]) < cut_len:
             # skip end edge case
             break
         rval = function(X_mb[start:stop], X_mb_mask[start:stop], prev_h1,
                         prev_h2, prev_h3)
         current_cost = rval[0]
         prev_h1, prev_h2, prev_h3 = rval[1:4]
         prev_h1 = prev_h1[-1]
         prev_h2 = prev_h2[-1]
         prev_h3 = prev_h3[-1]
     partial_costs.append(current_cost)
     print("")
     return partial_costs
            checkpoint_file = args.plot
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)
        train_costs = checkpoint_dict["train_costs"]
        valid_costs = checkpoint_dict["valid_costs"]
        plt.plot(train_costs)
        plt.plot(valid_costs)
        plt.savefig("costs.png")

        X_mb, X_mb_mask, c_mb, c_mb_mask = next(valid_itr)
        valid_itr.reset()
        prev_h1, prev_h2, prev_h3 = [
            np_zeros((minibatch_size, n_hid)) for i in range(3)
        ]
        prev_kappa = np_zeros((minibatch_size, att_size))
        prev_w = np_zeros((minibatch_size, n_chars))
        if args.sample is not None:
            predict_function = checkpoint_dict["predict_function"]
            attention_function = checkpoint_dict["attention_function"]
            sample_function = checkpoint_dict["sample_function"]
            if args.write is not None:
                sample_string = args.write
                print("Sampling using sample string %s" % sample_string)
                oh = dense_to_one_hot(
                    np.array([vocabulary[c] for c in sample_string]),
                    vocabulary_size)
                c_mb = np.zeros(
                    (len(oh), minibatch_size, oh.shape[-1])).astype(c_mb.dtype)
            checkpoint_file = args.plot
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)
        train_costs = checkpoint_dict["overall_train_costs"]
        valid_costs = checkpoint_dict["overall_valid_costs"]
        plt.plot(train_costs)
        plt.plot(valid_costs)
        plt.savefig("costs.png")

        X_mb, X_mb_mask, c_mb, c_mb_mask = next(valid_itr)
        valid_itr.reset()

        prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                     for i in range(3)]
        prev_kappa = np_zeros((minibatch_size, att_size))
        prev_w = np_zeros((minibatch_size, n_chars))
        bias = args.bias
        if args.sample is not None:
            predict_function = checkpoint_dict["predict_function"]
            attention_function = checkpoint_dict["attention_function"]
            sample_function = checkpoint_dict["sample_function"]
            if args.write is not None:
                sample_string = args.write
                print("Sampling using sample string %s" % sample_string)
                oh = dense_to_one_hot(
                    np.array([vocabulary[c] for c in sample_string]),
                    vocabulary_size)
                c_mb = np.zeros(
        else:
            checkpoint_file = args.plot
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)
        train_costs = checkpoint_dict["train_costs"]
        valid_costs = checkpoint_dict["valid_costs"]
        plt.plot(train_costs)
        plt.plot(valid_costs)
        plt.savefig("costs.png")

        X_mb, X_mb_mask, c_mb, c_mb_mask = next(valid_itr)
        valid_itr.reset()
        prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                     for i in range(3)]
        prev_kappa = np_zeros((minibatch_size, att_size))
        prev_w = np_zeros((minibatch_size, n_chars))
        if args.sample is not None:
            predict_function = checkpoint_dict["predict_function"]
            attention_function = checkpoint_dict["attention_function"]
            sample_function = checkpoint_dict["sample_function"]
            if args.write is not None:
                sample_string = args.write
                print("Sampling using sample string %s" % sample_string)
                oh = dense_to_one_hot(
                    np.array([vocabulary[c] for c in sample_string]),
                    vocabulary_size)
                c_mb = np.zeros(
                    (len(oh), minibatch_size, oh.shape[-1])).astype(c_mb.dtype)
Exemple #9
0
            checkpoint_file = args.plot
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)
        train_costs = checkpoint_dict["overall_train_costs"]
        valid_costs = checkpoint_dict["overall_valid_costs"]
        plt.plot(train_costs)
        plt.plot(valid_costs)
        plt.savefig("costs.png")

        X_mb, X_mb_mask, c_mb, c_mb_mask = next(train_itr)
        train_itr.reset()
        prev_h1, prev_h2, prev_h3 = [
            np_zeros((minibatch_size, n_hid)) for i in range(3)
        ]
        prev_kappa = np_zeros((minibatch_size, att_size))
        prev_w = np_zeros((minibatch_size, n_chars))
        bias = args.bias
        if args.sample is not None:
            predict_function = checkpoint_dict["predict_function"]
            attention_function = checkpoint_dict["attention_function"]
            sample_function = checkpoint_dict["sample_function"]
            if args.write is not None:
                sample_string = args.write
                print("Sampling using sample string %s" % sample_string)
                oh = dense_to_one_hot(
                    np.array([vocabulary[c] for c in sample_string]),
                    vocabulary_size)
                c_mb = np.zeros(
Exemple #10
0
        import matplotlib.pyplot as plt
        checkpoint_file = args.sample
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)
        train_costs = checkpoint_dict["train_costs"]
        valid_costs = checkpoint_dict["valid_costs"]
        plt.plot(train_costs)
        plt.plot(valid_costs)
        plt.savefig("costs.png")

        X_mb, X_mb_mask, y_mb, y_mb_mask = next(train_itr)
        train_itr.reset()
        prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                     for i in range(3)]

        predict_function = checkpoint_dict["predict_function"]
        sample_function = checkpoint_dict["sample_function"]

        if args.sample_length is None:
            raise ValueError("NYI - use -sl or --sample_length ")
        else:
            fixed_steps = args.sample_length
            completed = []
            init_x = np.zeros_like(X_mb[0]) + int(n_bins // 2)
            for i in range(fixed_steps):
                if i % 100 == 0:
                    print("Sampling step %i" % i)
                # remove second init_x later
                        required=False)
    args = parser.parse_args()
    if args.sample is not None:
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        checkpoint_file = args.sample
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)

        X_mb, X_mb_mask = next(train_itr)
        train_itr.reset()
        prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                     for i in range(3)]

        predict_function = checkpoint_dict["predict_function"]
        sample_function = checkpoint_dict["sample_function"]

        if args.temperature is None:
            args.temperature = 1.
        if args.sample_length is None:
            raise ValueError("NYI - use -sl or --sample_length ")
        else:
            fixed_steps = args.sample_length
            temperature = args.temperature
            completed = []
            # 0 is in the middle
            # CANNOT BE 1 timestep - will get floating point exception!
Exemple #12
0
                        required=False)
    args = parser.parse_args()
    if args.sample is not None:
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        checkpoint_file = args.sample
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)

        X_mb, X_mb_mask, c_mb, c_mb_mask = next(train_itr)
        train_itr.reset()
        prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                     for i in range(3)]
        prev_kappa = np_zeros((minibatch_size, n_att_size))
        prev_w = np_zeros((minibatch_size, n_chars))

        predict_function = checkpoint_dict["predict_function"]
        sample_function = checkpoint_dict["sample_function"]

        if args.temperature is None:
            args.temperature = 1.
        if args.sample_length is None:
            raise ValueError("NYI - use -sl or --sample_length ")
        else:
            sample_string = 'apple'
            print("Sampling using sample string %s" % sample_string)
            oh = speech_ds.dense_to_one_hot(
    args = parser.parse_args()
    if args.sample is not None:
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        checkpoint_file = args.sample
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)

        X_mb, X_mb_mask = next(train_itr)
        train_itr.reset()
        prev_h1, prev_h2, prev_h3 = [
            np_zeros((minibatch_size, n_hid)) for i in range(3)
        ]

        predict_function = checkpoint_dict["predict_function"]
        sample_function = checkpoint_dict["sample_function"]

        if args.temperature is None:
            args.temperature = 1.
        if args.sample_length is None:
            raise ValueError("NYI - use -sl or --sample_length ")
        else:
            fixed_steps = args.sample_length
            temperature = args.temperature
            completed = []
            # 0 is in the middle
            # CANNOT BE 1 timestep - will get floating point exception!
        else:
            checkpoint_file = args.plot
        if not os.path.exists(checkpoint_file):
            raise ValueError("Checkpoint file path %s" % checkpoint_file,
                             " does not exist!")
        print(checkpoint_file)
        checkpoint_dict = load_checkpoint(checkpoint_file)
        train_costs = checkpoint_dict["train_costs"]
        valid_costs = checkpoint_dict["valid_costs"]
        plt.plot(train_costs)
        plt.plot(valid_costs)
        plt.savefig("costs.png")

        X_mb, X_mb_mask, c_mb, c_mb_mask = next(valid_itr)
        valid_itr.reset()
        prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid))
                                     for i in range(3)]
        prev_kappa = np_zeros((minibatch_size, att_size))
        prev_w = np_zeros((minibatch_size, n_chars))
        if args.sample is not None:
            predict_function = checkpoint_dict["predict_function"]
            attention_function = checkpoint_dict["attention_function"]
            sample_function = checkpoint_dict["sample_function"]
            if args.write is not None:
                sample_string = args.write
                print("Sampling using sample string %s" % sample_string)
                oh = dense_to_one_hot(
                    np.array([vocabulary[c] for c in sample_string]),
                    vocabulary_size)
                c_mb = np.zeros(
                    (len(oh), minibatch_size, oh.shape[-1])).astype(c_mb.dtype)