I am stuck on this one piece of code because I can't get the generator to return me a the next value every time its called – it just stays on the first one! Take a look:
from numpy import *
def ArrayCoords(x,y,RowCount=0,ColumnCount=0): # I am trying to get it to print
while RowCount<x: # a new coordinate of a matrix
while ColumnCount<y: # left to right up to down each
yield (RowCount,ColumnCount) # time it's called.
ColumnCount+=1
RowCount+=1
ColumnCount=0
Here is what I get:
>>> next(ArrayCoords(20,20))
... (0, 0)
>>> next(ArrayCoords(20,20))
... (0, 0)
But it's just stuck on the first one! I expected this:
>>> next(ArrayCoords(20,20))
... (0, 0)
>>> next(ArrayCoords(20,20))
... (0, 1)
>>> next(ArrayCoords(20,20))
... (0, 2)
Could you guys help me out with the code as well as explain why it is so?
Thank you in advance!
Each time you call ArrayCoords(20,20) it returns a new generator object, different to the generator objects returned every other time you call ArrayCoords(20,20). To get the behaviour you want, you need to save the generator:
>>> coords = ArrayCoords(20,20)
>>> next(coords)
(0, 0)
>>> next(coords)
(0, 1)
>>> next(coords)
(0, 2)
You create a new generator on every line. Try this instead:
iterator = ArrayCoords(20, 20)
next(iterator)
next(iterator)
Related
I received error : too many values to unpack (expected 2) , when running the below code. anyone can help me? I added more details.
import gensim
import gensim.corpora as corpora
dictionary = corpora.Dictionary(doc_clean)
doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]
Lda = gensim.models.ldamodel.LdaModel
ldamodel = Lda(doc_term_matrix, num_topics=3, id2word = dictionary, passes=50, per_word_topics = True, eval_every = 1)
print(ldamodel.print_topics(num_topics=3, num_words=20))
for i in range (0,46):
for index, score in sorted(ldamodel[doc_term_matrix[i]], key=lambda tup: -1*tup[1]):
print("subject", i)
print("\n")
print("Score: {}\t \nTopic: {}".format(score, ldamodel.print_topic(index, 6)))
Focusing on the loop, since this is where the error is being raised. Let's take it one iteration at a time.
>>> import numpy as np # just so we can use np.shape()
>>> i = 0 # value in first loop
>>> x = sorted( ldamodel[doc_term_matrix[i]], key=lambda tup: -1*tup[1] )
>>> np.shape(x)
(3, 3, 2)
>>> for index, score in x:
... pass
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
Here is where your error is coming from. You are expecting this returned matrix to have 2 elements, however it is a multislice matrix with no simple infer-able way to unpack it. I do not personally have enough experience with this subject material to be able to infer what you might mean to be doing, I can only show you where your problem is coming from. Hope this helps!
I write this code and I have error in my subplot. I don't now what is wrong in my code. Can you help me ?
import pywt
import scipy.io.wavfile as wavfile
import matplotlib.pyplot as plt
rate,signal = wavfile.read('a0025.wav')
time = [x /rate for x in range(0,len(signal))]
tree = pywt.wavedec(data=signal[:1000], wavelet='db2', level=4, mode='symmetric')
print(len(tree))
newTree = [tree[0]*0, tree[1]*0, tree[2]*0, tree[3]*0, tree[4]]
recSignal = pywt.waverec(newTree,'db2')
fig, ax = plt.subplot(2, 1)
ax[0].plot(time[:1000], signal[:1000])
ax[0].set_xlabel('Czas [s]')
ax[0].set_ylabel('Amplituda')
ax[1].plot(time[:1000], recSignal[:1000])
ax[1].set_xlabel('Czas [s]')
ax[1].set_ylabel('Amplituda')
plt.show()
The error:
raise ValueError('Illegal argument(s) to subplot: %s' % (args,))
ValueError: Illegal argument(s) to subplot: (2, 1)
As the error clearly states, you passed an illegal argument to pyplot.subplot(). If you look at the documentation for that function, you'll see that it takes 3 arguments (which can be condensed in one): ax = plt.subplot(2, 1, 1) or ax = plt.subplot(211).
However, the function that you are looking for is plt.subplots() (note the s at the end), which generates both a figure and an array of subplots:
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
It seems that this bug is in the documentation see https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplots.html. They forgot the "s" in the third example. The first two examples are correct however.
e.g.
# using tuple unpacking for multiple Axes
fig, (ax1, ax2) = plt.subplot(1, 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2)
i am basically trying to build a deep model which consists of many convolution2d layers followed by maxpooling 2d as follows :
model.add(Convolution2D(128, 54, 7, input_shape=(1, 54, 180)))
model.add(MaxPooling2D(pool_size=(1, 3)))
model.add(Convolution2D(128, 1, 7))
model.add(MaxPooling2D(pool_size=(1, 3)))
However , i am getting the following error :
File
"/home/user/anaconda2/lib/python2.7/site-packages/keras/engine/training.py",
line 100, in standardize_input_data
str(array.shape)) Exception: Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array
with shape (8000, 180, 54)
Blockquote
But i am following the (samples, channels, rows, cols) norm. Why is this happening ?
It seems like your input data has the wrong shape. You should print out the shape of the data that you are feeding into the network.
It seems like your array are gray input images and they normally only use 2 dimensions because they only have 1 channel. Therefore the np array is ordered without the third dimension. Normally you have to add this by using np.reshape or allocating your array in another way. When I get an error message like yours i would try:
X # training data
X = np.transpose(X, (0, 2, 1))
X = np.reshape(X, (X.shape[0], 1, X.shape[1], X.shape[2]))
Hi I'm a beginner keras.
I'm making some model.
step 1. Input batch and word list, (BATCH_SIZE, WORD_INDEX_LIST)
step 2. Get word embeddings each words (BATCH_SIZE, WORD_LENGTH, EMBEDDING_SIZE)
step 3. Average each each word embeddings in each batch. (BATCH_SIZE, EMBEDDING_SIZE)
step 4. Repeat vector N, (BATCH_SIZE, N, EMBEDDING_SIZE)
step 5. Apply Dense Layer each time step
So, I write code.
MAX_LEN = 20 ( = WORD_INDEX_LIST)
step 1
layer_target_input = Input(shape=(MAX_LEN,), dtype="int32", name="layer_target_input")
# step2
layer_embedding = Embedding(input_dim = n_symbols+1, output_dim=vector_dim,input_length=MAX_LEN,
name="embedding", weights= [embedding_weights],trainable = False)
encoded_target = layer_embedding(layer_target_input)
# step 3
encoded_target_agg = KL.core.Lambda( lambda x: K.sum(x, axis=1) )(encoded_target)
#step 4
encoded_target_agg_repeat = KL.RepeatVector( MAX_LEN)(encoded_target_agg)
# step 5
layer_annotated_tahn = KL.Dense(output_dim=50, name="layer_tahn")
layer_annotated_tahn_td = KL.TimeDistributed(layer_annotated_tahn) (encoded_target_agg_repeat)
model = KM.Model(input=[layer_target_input], output=[ layer_annotated_tahn_td])
r = model.predict({ "layer_target_input":dev_targ}) # dev_targ = (2, 20, 300)
But, when i run this code,
result is bellow.
Traceback (most recent call last):
File "Main.py", line 127, in <module>
r = model.predict({ "layer_target_input":dev_targ})
File "/usr/local/anaconda/lib/python2.7/site-packages/Keras-1.0.7-py2.7.egg/keras/engine/training.py", line 1180, in predict
batch_size=batch_size, verbose=verbose)
File "/usr/local/anaconda/lib/python2.7/site-packages/Keras-1.0.7-py2.7.egg/keras/engine/training.py", line 888, in _predict_loop
outs[i][batch_start:batch_end] = batch_out
ValueError: could not broadcast input array from shape (30,20,50) into shape (2,20,50)
why batch size is changed?
What I have wrong?
The problem is in Lambda operator. In your case it takes a tensor of shape (batch_size, max_len, embedding_size) and is expected to produce a tensor of shape (batch_size, embedding_size). However, the Lambda op doesn't know what transformation you apply internally, and therefore during the graph compilation mistakenly assumes that the shape doesn't change, therefore assuming that the output shape is (batch_size, max_len, embedding_size). The RepeastVector that follows expects the input to be two-dimensional, but never asserts that it is the case. The way it produces the expected shape is (batch_size, num_repetitions, in_shape[1]). Since Lambda mistakenly reported its shape as (batch_size, max_len, embedding_size), RepeatVector now reports its shape as (batch_size, num_repetitions, max_len) instead of expected (batch_size, num_repetitions, embedding_size). num_repetitions in your case is the same as max_len, so RepeastVector reports its shape as (batch_size, max_len, max_len). The way TimeDistributed(Dense) works is:
Reshape((-1, input_shape[2]))
Dense()
Reshape((-1, input_shape[1], num_outputs))
By now input_shape[2] is mistakenly assumed to be max_len instead of embedding_size, but the actual tensor that is given has correct shape of (batch_size, max_len, embedding_size), so what ends up happening is:
Reshape((batch_size * embedding_size, max_len))
Dense()
Reshape((batch_size * embedding_size / max_len, max_len, num_outputs))
In your case batch_size * embedding_size / max_len happens to be 2 * 300 / 20 = 30, that's where your wrong shape comes from.
To fix it, you need to explicitly tell Lambda the shape you want it to produce:
encoded_target_agg = KL.core.Lambda( lambda x: K.sum(x, axis=1), output_shape=(vector_dim,))(encoded_target)
To teach myself PyMC I am trying to define a simple logistic regression. But I get a ZeroProbability error, and does not understand exactly why this happens or how to avoid it.
Here is my code:
import pymc
import numpy as np
x = np.array([85, 95, 70, 65, 70, 90, 75, 85, 80, 85])
y = np.array([1., 1., 0., 0., 0., 1., 1., 0., 0., 1.])
w0 = pymc.Normal('w0', 0, 0.000001) # uninformative prior (any real number)
w1 = pymc.Normal('w1', 0, 0.000001) # uninformative prior (any real number)
#pymc.deterministic
def logistic(w0=w0, w1=w1, x=x):
return 1.0 / (1. + np.exp(-(w0 + w1 * x)))
observed = pymc.Bernoulli('observed', logistic, value=y, observed=True)
And here is the trace back with the error message:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/IPython/core/interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-43ed68985dd1>", line 24, in <module>
observed = pymc.Bernoulli('observed', logistic, value=y, observed=True)
File "/usr/local/lib/python2.7/site-packages/pymc/distributions.py", line 318, in __init__
**arg_dict_out)
File "/usr/local/lib/python2.7/site-packages/pymc/PyMCObjects.py", line 772, in __init__
if not isinstance(self.logp, float):
File "/usr/local/lib/python2.7/site-packages/pymc/PyMCObjects.py", line 929, in get_logp
raise ZeroProbability(self.errmsg)
ZeroProbability: Stochastic observed's value is outside its support,
or it forbids its parents' current values.
I suspect np.exp to be causing the trouble, since it returns inf when the linear equation becomes too high.
I know there are other ways to define a logistic regression using PyMC (her is one), but I am interested in knowing why this approach does not work, and how I can define the regression using the Bernoulli object instead of using bernoulli_like
When you create a your normal stochastastic with pymc.Normal('w0', 0, 0.000001), PyMC2 initializes the value with a random draw from the prior distribution. Since your prior is so diffuse, this can be a value which is so unlikely that the posterior is effectively zero. To fix, just request a reasonable initial value for your Normal:
w0 = pymc.Normal('w0', 0, 0.000001, value=0)
w1 = pymc.Normal('w1', 0, 0.000001, value=0)
Here is a notebook with a few more details.
You have to put some sort of bound on the probability returned by the logistic function.
Maybe something like
#pymc.deterministic
def logistic(w0=w0, w1=w1, x=x):
tol = 1e-9
res = 1.0 / (1. + np.exp(-(w0 + w1 * x)))
return np.maximum(np.minimum(res, 1 - tol), tol)
I think you forgot the negative inside the exp() function, too.
#hahdawg's answer is good, but here's something else to consider.
For your uninformative priors on w0 and w1 I would first do an eyeball fit and then use uniforms with limits.
Obviously your w1 is going to be around 1/15 = .07, so a range like .04 to 1.2 might do it.
w0 is going to be in the range of -80/15 = -5.3, so something like -7 to -3 could do it.
I'm just saying this because exp can easily go bananas, so you have to be careful what you feed it.
If your inverse logit function comes out with a value too close to 0 or 1, logistic regression is guaranteed to break.
Out of curiosity, are you using a thin argument in your call to sample? There was a bug related to that, and it may be the culprit here.
Besides, thinning is not worthwhile in any case.