I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Explore the Data
Play around with view_sentence_range to view different parts of the data.
Can you write Python code for it?

view_sentence_range = (0, 10)

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

print('Dataset Stats')
print('Roughly the number of unique words: {}'.format(len({word: None for word in text.split()})))
scenes = text.split('\n\n')
print('Number of scenes: {}'.format(len(scenes)))
sentence_count_scene = [scene.count('\n') for scene in scenes]
print('Average number of sentences in each scene: {}'.format(np.average(sentence_count_scene)))

sentences = [sentence for scene in scenes for sentence in scene.split('\n')]
print('Number of lines: {}'.format(len(sentences)))
word_count_sentence = [len(sentence.split()) for sentence in sentences]
print('Average number of words in each line: {}'.format(np.average(word_count_sentence)))

print()
print('The sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Generate TV Script
This will generate the TV script for you.  Set gen_length to the length of TV script you want to generate.
Can you write Python code for it?

gen_length = 200
# homer_simpson, moe_szyslak, or Barney_Gumble
prime_word = 'moe_szyslak'

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
    # Load saved model
    loader = tf.train.import_meta_graph(load_dir + '.meta')
    loader.restore(sess, load_dir)

    # Get Tensors from loaded model
    input_text, initial_state, final_state, probs = get_tensors(loaded_graph)

    # Sentences generation setup
    gen_sentences = [prime_word + ':']
    prev_state = sess.run(initial_state, {input_text: np.array([[1]])})

    # Generate sentences
    for n in range(gen_length):
        # Dynamic Input
        dyn_input = [[vocab_to_int[word] for word in gen_sentences[-seq_length:]]]
        dyn_seq_length = len(dyn_input[0])

        # Get Prediction
        probabilities, prev_state = sess.run(
            [probs, final_state],
            {input_text: dyn_input, initial_state: prev_state})

        pred_word = pick_word(probabilities[dyn_seq_length-1], int_to_vocab)

        gen_sentences.append(pred_word)

    # Remove tokens
    tv_script = ' '.join(gen_sentences)
    for key, token in token_dict.items():
        ending = ' ' if key in ['\n', '(', '"'] else ''
        tv_script = tv_script.replace(' ' + token.lower(), key)
    tv_script = tv_script.replace('\n ', '\n')
    tv_script = tv_script.replace('( ', '(')

    print(tv_script)


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Implement Preprocessing Functions
The first thing to do to any dataset is preprocessing.  Implement the following preprocessing functions below:
- Lookup Table
- Tokenize Punctuation
Lookup Table
To create a word embedding, you first need to transform the words to ids.  In this function, create two dictionaries:
- Dictionary to go from the words to an id, we'll call vocab_to_int
- Dictionary to go from the id to word, we'll call int_to_vocab
Return these dictionaries in the following tuple (vocab_to_int, int_to_vocab)
Can you write Python code for it?

import numpy as np
from collections import Counter
import problem_unittests as tests

def create_lookup_tables(text):
    """
    Create lookup tables for vocabulary
    :param text: The text of tv scripts split into words
    :return: A tuple of dicts (vocab_to_int, int_to_vocab)
    """
    # TODO: Implement Function
    word_counts = Counter(text)
    sorted_vocab = sorted(word_counts, key=word_counts.get, reverse=True)
    int_to_vocab = {index: word for index, word in enumerate(sorted_vocab)}
    vocab_to_int = {word: index for index, word in int_to_vocab.items()}
    return vocab_to_int, int_to_vocab

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_create_lookup_tables(create_lookup_tables)


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Neural Network Training
Hyperparameters
Tune the following parameters:

Set num_epochs to the number of epochs.
Set batch_size to the batch size.
Set rnn_size to the size of the RNNs.
Set embed_dim to the size of the embedding.
Set seq_length to the length of sequence.
Set learning_rate to the learning rate.
Set show_every_n_batches to the number of batches the neural network should print progress.
Can you write Python code for it?

# Number of Epochs
num_epochs = 100
# Batch Size
batch_size = 500
# RNN Size
rnn_size = 256
# Embedding Dimension Size
embed_dim = 300
# Sequence Length
seq_length = 25
# Learning Rate
learning_rate = 0.01
# Show stats for every n number of batches
show_every_n_batches = 20

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
save_dir = './save'


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Preprocess all the data and save it
Running the code cell below will preprocess all the data and save it to file.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Preprocess Training, Validation, and Testing Data
helper.preprocess_and_save_data(data_dir, token_lookup, create_lookup_tables)


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Save Parameters
Save seq_length and save_dir for generating a new TV script.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Save parameters for checkpoint
helper.save_params((seq_length, save_dir))


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Tokenize Punctuation
We'll be splitting the script into a word array using spaces as delimiters.  However, punctuations like periods and exclamation marks make it hard for the neural network to distinguish between the word "bye" and "bye!".
Implement the function token_lookup to return a dict that will be used to tokenize symbols like "!" into "||Exclamation_Mark||".  Create a dictionary for the following symbols where the symbol is the key and value is the token:
- Period ( . )
- Comma ( , )
- Quotation Mark ( " )
- Semicolon ( ; )
- Exclamation mark ( ! )
- Question mark ( ? )
- Left Parentheses ( ( )
- Right Parentheses ( ) )
- Dash ( -- )
- Return ( \n )
This dictionary will be used to token the symbols and add the delimiter (space) around it.  This separates the symbols as it's own word, making it easier for the neural network to predict on the next word. Make sure you don't use a token that could be confused as a word. Instead of using the token "dash", try using something like "||dash||".
Can you write Python code for it?

def token_lookup():
    """
    Generate a dict to turn punctuation into a token.
    :return: Tokenize dictionary where the key is the punctuation and the value is the token
    """
    # TODO: Implement Function
    token_dict = {}
    token_dict['.'] = '||Period||'
    token_dict[','] = '||Comma||'
    token_dict['"'] = '||Quotation_Mark||'
    token_dict[';'] = '||Semicolon||'
    token_dict['!'] = '||Exclamation_Mark||'
    token_dict['?'] = '||Question_Mark||'
    token_dict['('] = '||Left_Parentheses||'
    token_dict[')'] = '||Right_Parentheses||'
    token_dict['--'] = '||Dash||'
    token_dict['\n'] = '||Return||'

    return token_dict

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_tokenize(token_lookup)


I am working on the file "Project3/Generate_TV_Scripts/dlnd_tv_script_generation.ipynb".
The first task is:
Train
Train the neural network on the preprocessed data.  If you have a hard time getting a good loss, check the forms to see if anyone is having the same problem.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
batches = get_batches(int_text, batch_size, seq_length)

with tf.Session(graph=train_graph) as sess:
    sess.run(tf.global_variables_initializer())

    for epoch_i in range(num_epochs):
        state = sess.run(initial_state, {input_text: batches[0][0]})

        for batch_i, (x, y) in enumerate(batches):
            feed = {
                input_text: x,
                targets: y,
                initial_state: state,
                lr: learning_rate}
            train_loss, state, _ = sess.run([cost, final_state, train_op], feed)

            # Show every <show_every_n_batches> batches
            if (epoch_i * len(batches) + batch_i) % show_every_n_batches == 0:
                print('Epoch {:>3} Batch {:>4}/{}   train_loss = {:.3f}'.format(
                    epoch_i,
                    batch_i,
                    len(batches),
                    train_loss))

    # Save Model
    saver = tf.train.Saver()
    saver.save(sess, save_dir)
    print('Model Trained and Saved')


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
分析数据
在这部分，你将开始分析数据，通过可视化和代码来理解每一个特征和其他特征的联系。你会看到关于数据集的统计描述，考虑每一个属性的相关性，然后从数据集中选择若干个样本数据点，你将在整个项目中一直跟踪研究这几个数据点。
运行下面的代码单元给出数据集的一个统计描述。注意这个数据集包含了6个重要的产品类型：'Fresh', 'Milk', 'Grocery', 'Frozen', 'Detergents_Paper'和 'Delicatessen'。想一下这里每一个类型代表你会购买什么样的产品。
Can you write Python code for it?


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
可视化一个双标图（Biplot）
双标图是一个散点图，每个数据点的位置由它所在主成分的分数确定。坐标系是主成分（这里是Dimension 1 和 Dimension 2）。此外，双标图还展示出初始特征在主成分上的投影。一个双标图可以帮助我们理解降维后的数据，发现主成分和初始特征之间的关系。
运行下面的代码来创建一个降维后数据的双标图。
Can you write Python code for it?

# Create a biplot
vs.biplot(good_data, reduced_data, pca)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
回答:
不相符,三个点都预测结果都是Cluster 0，这与感性认知的结论有比较大的冲突，说明自行预测的时候还是缺乏经验，无法正确预测
结论
在最后一部分中，你要学习如何使用已经被分类的数据。首先，你要考虑不同组的客户客户分类，针对不同的派送策略受到的影响会有什么不同。其次，你要考虑到，每一个客户都被打上了标签（客户属于哪一个分类）可以给客户数据提供一个多一个特征。最后，你会把客户分类与一个数据中的隐藏变量做比较，看一下这个分类是否辨识了特定的关系。
问题 10
在对他们的服务或者是产品做细微的改变的时候，公司经常会使用A/B tests以确定这些改变会对客户产生积极作用还是消极作用。这个批发商希望考虑将他的派送服务从每周5天变为每周3天，但是他只会对他客户当中对此有积极反馈的客户采用。这个批发商应该如何利用客户分类来知道哪些客户对它的这个派送策略的改变有积极的反馈，如果有的话？你需要给出在这个情形下A/B 测试具体的实现方法，以及最终得出结论的依据是什么？
提示： 我们能假设这个改变对所有的客户影响都一致吗？我们怎样才能够确定它对于哪个类型的客户影响最大？
回答：
从每个分类中各找出相等的一小部分客户，然后给他们派送的服务从每周5天变成3天，经过两周的实验，观察哪个分类的用户反馈更积极
如果其中某些分类的客户基本没有退订服务，那么说明这些客户改成3天没有问题
如果某些分类的客户退订服务很多，那么说明这些客户还应该每周派送5天
可以先假设都一致，根据退订的比例来确定对那个类型的客户影响最大
问题 11
通过聚类技术，我们能够将原有的没有标记的数据集中的附加结构分析出来。因为每一个客户都有一个最佳的划分（取决于你选择使用的聚类算法），我们可以把用户分类作为数据的一个工程特征。假设批发商最近迎来十位新顾客，并且他已经为每位顾客每个产品类别年度采购额进行了预估。进行了这些估算之后，批发商该如何运用它的预估和非监督学习的结果来对这十个新的客户进行更好的预测？
提示：在下面的代码单元中，我们提供了一个已经做好聚类的数据（聚类结果为数据中的cluster属性），我们将在这个数据集上做一个小实验。尝试运行下面的代码看看我们尝试预测‘Region’的时候，如果存在聚类特征'cluster'与不存在相比对最终的得分会有什么影响？这对你有什么启发？
Can you write Python code for it?

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# 读取包含聚类结果的数据
cluster_data = pd.read_csv("cluster.csv")
y = cluster_data['Region']
X = cluster_data.drop(['Region'], axis = 1)

# 划分训练集测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=24)

clf = RandomForestClassifier(random_state=24)
clf.fit(X_train, y_train)
print "使用cluster特征的得分", clf.score(X_test, y_test)

# 移除cluster特征
X_train = X_train.copy()
X_train.drop(['cluster'], axis=1, inplace=True)
X_test = X_test.copy()
X_test.drop(['cluster'], axis=1, inplace=True)
clf.fit(X_train, y_train)
print "不使用cluster特征的得分", clf.score(X_test, y_test)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
回答：
cluster的存在会使得分变高，有可能预测会变得更准确
遇到问题时，有时候并不需要刻板的使用某个方法，或者某一类技术，应该尽力解决问题，开拓思路。非监督学习的结论可以作用于监督学习。。。
可视化内在的分布
在这个项目的开始，我们讨论了从数据集中移除'Channel'和'Region'特征，这样在分析过程中我们就会着重分析用户产品类别。通过重新引入Channel这个特征到数据集中，并施加和原来数据集同样的PCA变换的时候我们将能够发现数据集产生一个有趣的结构。
运行下面的代码单元以查看哪一个数据点在降维的空间中被标记为'HoReCa' (旅馆/餐馆/咖啡厅)或者'Retail'。另外，你将发现样本点在图中被圈了出来，用以显示他们的标签。
Can you write Python code for it?

# 根据‘Channel‘数据显示聚类的结果
vs.channel_results(reduced_data, outliers, pca_samples)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
练习: 异常值检测
对于任何的分析，在数据预处理的过程中检测数据中的异常值都是非常重要的一步。异常值的出现会使得把这些值考虑进去后结果出现倾斜。这里有很多关于怎样定义什么是数据集中的异常值的经验法则。这里我们将使用Tukey的定义异常值的方法：一个异常阶（outlier step）被定义成1.5倍的四分位距（interquartile range，IQR）。一个数据点如果某个特征包含在该特征的IQR之外的特征，那么该数据点被认定为异常点。
在下面的代码单元中，你需要完成下面的功能：
 - 将指定特征的25th分位点的值分配给Q1。使用np.percentile来完成这个功能。
 - 将指定特征的75th分位点的值分配给Q3。同样的，使用np.percentile来完成这个功能。
 - 将指定特征的异常阶的计算结果赋值给step.
 - 选择性地通过将索引添加到outliers列表中，以移除异常值。
注意： 如果你选择移除异常值，请保证你选择的样本点不在这些移除的点当中！
一旦你完成了这些功能，数据集将存储在good_data中。
Can you write Python code for it?

# 对于每一个特征，找到值异常高或者是异常低的数据点
for feature in log_data.keys():

    # TODO：计算给定特征的Q1（数据的25th分位点）
    Q1 = np.percentile(log_data[feature],25)

    # TODO：计算给定特征的Q3（数据的75th分位点）
    Q3 = np.percentile(log_data[feature],75)

    # TODO：使用四分位范围计算异常阶（1.5倍的四分位距）
    step = (Q3-Q1)*1.5

    # 显示异常点
    print "Data points considered outliers for the feature '{}':".format(feature)
    display(log_data[~((log_data[feature] >= Q1 - step) & (log_data[feature] <= Q3 + step))])

# 可选：选择你希望移除的数据点的索引
outliers  = []

# 如果选择了的话，移除异常点
good_data = log_data.drop(log_data.index[outliers]).reset_index(drop = True)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
练习: 数据恢复
上面的可视化图像中提供的每一个聚类都有一个中心点。这些中心（或者叫平均点）并不是数据中真实存在的点，但是是所有预测在这个簇中的数据点的平均。对于创建客户分类的问题，一个簇的中心对应于那个分类的平均用户。因为这个数据现在进行了降维并缩放到一定的范围，我们可以通过施加一个反向的转换恢复这个点所代表的用户的花费。
在下面的代码单元中，你将实现下列的功能：
 - 使用pca.inverse_transform将centers 反向转换，并将结果存储在log_centers中。
 - 使用np.log的反函数np.exp反向转换log_centers并将结果存储到true_centers中。
Can you write Python code for it?

# TODO：反向转换中心点
log_centers = pca.inverse_transform(centers)

# TODO：对中心点做指数转换
true_centers = np.exp(log_centers)

# 显示真实的中心点
segments = ['Segment {}'.format(i) for i in range(0,len(centers))]
true_centers = pd.DataFrame(np.round(true_centers), columns = data.keys())
true_centers.index = segments
display(true_centers)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
练习：降维
当使用主成分分析的时候，一个主要的目的是减少数据的维度，这实际上降低了问题的复杂度。当然降维也是需要一定代价的：更少的维度能够表示的数据中的总方差更少。因为这个，累计解释方差比（cumulative explained variance ratio）对于我们确定这个问题需要多少维度非常重要。另外，如果大部分的方差都能够通过两个或者是三个维度进行表示的话，降维之后的数据能够被可视化。
在下面的代码单元中，你将实现下面的功能：
 - 将good_data用两个维度的PCA进行拟合，并将结果存储到pca中去。
 - 使用pca.transform将good_data进行转换，并将结果存储在reduced_data中。
 - 使用pca.transform将log_samples进行转换，并将结果存储在pca_samples中。
Can you write Python code for it?

# TODO：通过在good data上进行PCA，将其转换成两个维度
pca = PCA(n_components=2).fit(good_data)

# TODO：使用上面训练的PCA将good data进行转换
reduced_data = pca.transform(good_data)

# TODO：使用上面训练的PCA将log_samples进行转换
pca_samples = pca.transform(log_samples)

# 为降维后的数据创建一个DataFrame
reduced_data = pd.DataFrame(reduced_data, columns = ['Dimension 1', 'Dimension 2'])


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
观察
一旦我们有了原始特征的投影（红色箭头），就能更加容易的理解散点图每个数据点的相对位置。
在这个双标图中，哪些初始特征与第一个主成分有强关联？哪些初始特征与第二个主成分相关联？你观察到的是否与之前得到的 pca_results 图相符？
聚类
在这个部分，你讲选择使用K-Means聚类算法或者是高斯混合模型聚类算法以发现数据中隐藏的客户分类。然后，你将从簇中恢复一些特定的关键数据点，通过将它们转换回原始的维度和规模，从而理解他们的含义。
问题 6
使用K-Means聚类算法的优点是什么？使用高斯混合模型聚类算法的优点是什么？基于你现在对客户数据的观察结果，你选用了这两个算法中的哪一个，为什么？
回答:
K-Means
1. K-Means聚类方法主要是方便，简单。
2. 算法收敛快，且便于分布式实施。
3. 调参方便，基本固定K值之后只需要简单调整即可。
高斯混合模型
1. K-Means假定了数据呈圆形或高维球形分布，而高斯混合模型假设数据是呈正态分布的，更符合自然届数据类型
2. K-Means得出的聚类只有是或否的硬聚类，而高斯混合模型得出的是软聚类，可以量化数据
我选用K-Means，目前并不需要做量化软聚类，且目前数据较为简单。
练习: 创建聚类
针对不同情况，有些问题你需要的聚类数目可能是已知的。但是在聚类数目不作为一个先验知道的情况下，我们并不能够保证某个聚类的数目对这个数据是最优的，因为我们对于数据的结构（如果存在的话）是不清楚的。但是，我们可以通过计算每一个簇中点的轮廓系数来衡量聚类的质量。数据点的轮廓系数衡量了它与分配给他的簇的相似度，这个值范围在-1（不相似）到1（相似）。平均轮廓系数为我们提供了一种简单地度量聚类质量的方法。
在接下来的代码单元中，你将实现下列功能：
 - 在reduced_data上使用一个聚类算法，并将结果赋值到clusterer，需要设置 random_state 使得结果可以复现。
 - 使用clusterer.predict预测reduced_data中的每一个点的簇，并将结果赋值到preds。
 - 使用算法的某个属性值找到聚类中心，并将它们赋值到centers。
 - 预测pca_samples中的每一个样本点的类别并将结果赋值到sample_preds。
 - 导入sklearn.metrics.silhouette_score包并计算reduced_data相对于preds的轮廓系数。
   - 将轮廓系数赋值给score并输出结果。
Can you write Python code for it?

from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

# TODO：在降维后的数据上使用你选择的聚类算法
clusterer = KMeans(n_clusters=2, random_state=29).fit(reduced_data)

# TODO：预测每一个点的簇
preds =  clusterer.predict(reduced_data)

# TODO：找到聚类中心
centers = clusterer.cluster_centers_

# TODO：预测在每一个转换后的样本点的类
sample_preds = clusterer.predict(pca_samples)

# TODO：计算选择的类别的平均轮廓系数（mean silhouette coefficient）
score = silhouette_score(reduced_data, clusterer.labels_, metric='euclidean')

print score
# 2 => 0.419166083203
# 3 => 0.392358689958
# 4 => 0.333872503172
# 5 => 0.333872503172
# 6 => 0.359310196851


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
观察
在使用了一个自然对数的缩放之后，数据的各个特征会显得更加的正态分布。对于任意的你以前发现有相关关系的特征对，观察他们的相关关系是否还是存在的（并且尝试观察，他们的相关关系相比原来是变强了还是变弱了）。
运行下面的代码以观察样本数据在进行了自然对数转换之后如何改变了。
Can you write Python code for it?


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
观察
运行以下代码观察当仅仅使用两个维度进行PCA转换后，这个对数样本数据将怎样变化。观察这里的结果与一个使用六个维度的PCA转换相比较时，前两维的数值是保持不变的。
Can you write Python code for it?

# 展示经过两个维度的PCA转换之后的样本log-data
display(pd.DataFrame(np.round(pca_samples, 4), columns = ['Dimension 1', 'Dimension 2']))


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
问题 1
在你看来你选择的这三个样本点分别代表什么类型的企业（客户）？对每一个你选择的样本客户，通过它在每一种产品类型上的花费与数据集的统计描述进行比较，给出你做上述判断的理由。
提示： 企业的类型包括超市、咖啡馆、零售商以及其他。注意不要使用具体企业的名字，比如说在描述一个餐饮业客户时，你不能使用麦当劳。
回答:
20:这个客户买了很多fresh，超过了75%的客户，牛奶和少量蔬菜，还有很多熟食，像是一个超市
60：这个用户相比之下，冷冻和熟食很少低于25%的用户，Grocery很多，像是一个街边零售商
160：这个客户Grocery很多，熟食也很多（超过75%），像是只对食材粗加工，更像一个咖啡馆
练习: 特征相关性
一个有趣的想法是，考虑这六个类别中的一个（或者多个）产品类别，是否对于理解客户的购买行为具有实际的相关性。也就是说，当用户购买了一定数量的某一类产品，我们是否能够确定他们必然会成比例地购买另一种类的产品。有一个简单的方法可以检测相关性：我们用移除了某一个特征之后的数据集来构建一个监督学习（回归）模型，然后用这个模型去预测那个被移除的特征，再对这个预测结果进行评分，看看预测结果如何。
在下面的代码单元中，你需要实现以下的功能：
 - 使用DataFrame.drop函数移除数据集中你选择的不需要的特征，并将移除后的结果赋值给new_data。
 - 使用sklearn.model_selection.train_test_split将数据集分割成训练集和测试集。
   - 使用移除的特征作为你的目标标签。设置test_size为0.25并设置一个random_state。
 - 导入一个DecisionTreeRegressor（决策树回归器），设置一个random_state，然后用训练集训练它。
 - 使用回归器的score函数输出模型在测试集上的预测得分。
Can you write Python code for it?

from sklearn.cross_validation import train_test_split
from sklearn import tree
from sklearn.metrics import r2_score
# TODO：为DataFrame创建一个副本，用'drop'函数丢弃一个特征
detergents_array = data['Detergents_Paper']
new_data = data.drop(['Detergents_Paper'],axis = 1)

# TODO：使用给定的特征作为目标，将数据分割成训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(new_data, detergents_array,
                                                    test_size=0.25, random_state=23)

# TODO：创建一个DecisionTreeRegressor（决策树回归器）并在训练集上训练它
regressor = tree.DecisionTreeRegressor(random_state=29)
regressor.fit(X_train, y_train)

# TODO：输出在测试集上的预测得分
score = regressor.score(X_test, y_test)
print("Score Is ", score)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
问题 2
你尝试预测哪一个特征？预测的得分是多少？这个特征对于区分用户的消费习惯来说必要吗？
提示： 决定系数（coefficient of determination）, R^2,结果在0到1之间，1表示完美拟合，一个负的R^2表示模型不能够拟合数据。
回答:尝试预测了Detergents_Paper，得分0.76,这个分数来讲已经比较高，说明此模型已经能大体上拟合数据。
这个特征对于区分消费者习惯不是很必要，因为我们可以通过其他的因素来预测出Detergents_Paper的购买，因此Detergents_Paper不是区分消费者的主要因素
可视化特征分布
为了能够对这个数据集有一个更好的理解，我们可以对数据集中的每一个产品特征构建一个散布矩阵（scatter matrix）。如果你发现你在上面尝试预测的特征对于区分一个特定的用户来说是必须的，那么这个特征和其它的特征可能不会在下面的散射矩阵中显示任何关系。相反的，如果你认为这个特征对于识别一个特定的客户是没有作用的，那么通过散布矩阵可以看出在这个数据特征和其它特征中有关联性。运行下面的代码以创建一个散布矩阵。
Can you write Python code for it?

# 对于数据中的每一对特征构造一个散布矩阵
pd.plotting.scatter_matrix(data, alpha = 0.3, figsize = (14,8), diagonal = 'kde');


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
问题 4
请列出所有在多于一个特征下被看作是异常的数据点。这些点应该被从数据集中移除吗？为什么？把你认为需要移除的数据点全部加入到到outliers变量中。
回答:
65,66,75,128 个人觉得不应该，在这种场景中，根据四分位确定的异常值，很可能已经是代表了某种顾客的特征，多个特征均被视为异常值，反而可能是一种规律。反正现在是看不出来什么异常。
特征转换
在这个部分中你将使用主成分分析（PCA）来分析批发商客户数据的内在结构。由于使用PCA在一个数据集上会计算出最大化方差的维度，我们将找出哪一个特征组合能够最好的描绘客户。
练习: 主成分分析（PCA）
既然数据被缩放到一个更加正态分布的范围中并且我们也移除了需要移除的异常点，我们现在就能够在good_data上使用PCA算法以发现数据的哪一个维度能够最大化特征的方差。除了找到这些维度，PCA也将报告每一个维度的解释方差比（explained variance ratio）--这个数据有多少方差能够用这个单独的维度来解释。注意PCA的一个组成部分（维度）能够被看做这个空间中的一个新的“特征”，但是它是原来数据中的特征构成的。
在下面的代码单元中，你将要实现下面的功能：
 - 导入sklearn.decomposition.PCA并且将good_data用PCA并且使用6个维度进行拟合后的结果保存到pca中。
 - 使用pca.transform将log_samples进行转换，并将结果存储到pca_samples中。
Can you write Python code for it?

from sklearn.decomposition import PCA
# TODO：通过在good_data上使用PCA，将其转换成和当前特征数一样多的维度
pca = PCA(n_components=len(good_data.columns)).fit(good_data)

print log_samples

# TODO：使用上面的PCA拟合将变换施加在log_samples上
pca_samples = pca.transform(log_samples)

print pca_samples

# 生成PCA的结果图
pca_results = vs.pca_results(good_data, pca)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
问题 5
数据的第一个和第二个主成分 总共 表示了多少的方差？  前四个主成分呢？使用上面提供的可视化图像，讨论从用户花费的角度来看前四个主要成分的消费行为最能代表哪种类型的客户并给出你做出判断的理由。
提示： 某一特定维度上的正向增长对应正权特征的增长和负权特征的减少。增长和减少的速率和每个特征的权重相关。参考资料(英文)。
回答: 第一、第二个主成分表示了71%左右的方差，前四个之和有90%左右。
从第一个维度的图来看，Detergents_Paper、Grocery、Milk占了很大的比重，很有可能是食品杂货店之类的客户。
第二个维度中，Detergents_Paper、Grocery、Milk比重完全降低，而Fresh、Frozen、Delicatessen，比重又占了很多，应该和维度1几乎正交，推测为熟食店。
第三个比重中，Fresh、Delicatessen比重又很大，像是前两者不完全涵盖的类型。
观察
运行下面的代码，查看经过对数转换的样本数据在进行一个6个维度的主成分分析（PCA）之后会如何改变。观察样本数据的前四个维度的数值。考虑这和你初始对样本点的解释是否一致。
Can you write Python code for it?

# 展示经过PCA转换的sample log-data
display(pd.DataFrame(np.round(pca_samples, 4), columns = pca_results.index.values))


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
问题 7
汇报你尝试的不同的聚类数对应的轮廓系数。在这些当中哪一个聚类的数目能够得到最佳的轮廓系数？
回答:
试了从2=>6几个K值，最佳的为2
聚类可视化
一旦你选好了通过上面的评价函数得到的算法的最佳聚类数目，你就能够通过使用下面的代码块可视化来得到的结果。作为实验，你可以试着调整你的聚类算法的聚类的数量来看一下不同的可视化结果。但是你提供的最终的可视化图像必须和你选择的最优聚类数目一致。
Can you write Python code for it?

# 从已有的实现中展示聚类的结果
vs.cluster_results(reduced_data, preds, centers, pca_samples)


I am working on the file "Project3/customer_segments.ipynb".
The first task is:
问题 8
考虑上面的代表性数据点在每一个产品类型的花费总数，你认为这些客户分类代表了哪类客户？为什么？需要参考在项目最开始得到的统计值来给出理由。
提示： 一个被分到'Cluster X'的客户最好被用 'Segment X'中的特征集来标识的企业类型表示。
回答:
Cluster 0更像是零售商，因为他们的中心点Segment 0的各项特征值都是50%左右，低于平均值，不存在某个特征特别高，而量又没有特别大，符合零售商什么都卖，但是入货不算多的特点
Cluster 1更像是咖啡店，并且他们的中心点Segment 1的值牛奶和、杂物和清洁纸的量超过了75%而且超过了平均数，而其他的数量较少，咖啡店牛奶和纸巾的消耗必然很多，所以更有可能是咖啡店
问题 9
对于每一个样本点 * 问题 8  中的哪一个分类能够最好的表示它？你之前对样本的预测和现在的结果相符吗？*
运行下面的代码单元以找到每一个样本点被预测到哪一个簇中去。
Can you write Python code for it?

# 显示预测结果
for i, pred in enumerate(sample_preds):
    print "Sample point", i, "predicted to be in Cluster", pred


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Batches
Implement get_batches to create batches of input and targets using int_text.  The batches should be a Numpy array with the shape (number of batches, 2, batch size, sequence length). Each batch contains two elements:
- The first element is a single batch of input with the shape [batch size, sequence length]
- The second element is a single batch of targets with the shape [batch size, sequence length]
If you can't fill the last batch with enough data, drop the last batch.
For exmple, get_batches([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 3, 2) would return a Numpy array of the following:
```
[
  # First Batch
  [
    # Batch of Input
    [[ 1  2], [ 7  8], [13 14]]
    # Batch of targets
    [[ 2  3], [ 8  9], [14 15]]
  ]
# Second Batch
  [
    # Batch of Input
    [[ 3  4], [ 9 10], [15 16]]
    # Batch of targets
    [[ 4  5], [10 11], [16 17]]
  ]
# Third Batch
  [
    # Batch of Input
    [[ 5  6], [11 12], [17 18]]
    # Batch of targets
    [[ 6  7], [12 13], [18  1]]
  ]
]
```
Notice that the last target value in the last batch is the first input value of the first batch. In this case, 1. This is a common technique used when creating sequence batches, although it is rather unintuitive.
Can you write Python code for it?

def get_batches(int_text, batch_size, seq_length):
    """
    Return batches of input and target
    :param int_text: Text with the words replaced by their ids
    :param batch_size: The size of batch
    :param seq_length: The length of sequence
    :return: Batches as a Numpy array
    """
    # TODO: Implement Function
    num_batch = int(len(int_text)/(batch_size*seq_length))

    in_data = np.array(int_text[:num_batch*batch_size*seq_length])
    out_data = np.append(int_text[1:num_batch*batch_size*seq_length],int_text[0])

    in_batches = np.split(in_data.reshape(batch_size,-1),num_batch,1)
    out_batches = np.split(out_data.reshape(batch_size,-1),num_batch,1)

    batchPair = np.array(list(zip(in_batches,out_batches)))
    return batchPair

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_batches(get_batches)


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Build RNN
You created a RNN Cell in the get_init_cell() function.  Time to use the cell to create a RNN.
- Build the RNN using the tf.nn.dynamic_rnn()
 - Apply the name "final_state" to the final state using tf.identity()
Return the outputs and final_state state in the following tuple (Outputs, FinalState)
Can you write Python code for it?

def build_rnn(cell, inputs):
    """
    Create a RNN using a RNN Cell
    :param cell: RNN Cell
    :param inputs: Input text data
    :return: Tuple (Outputs, Final State)
    """
    # TODO: Implement Function
    outputs,final_state = tf.nn.dynamic_rnn(cell,inputs,dtype=tf.float32)
    final_state = tf.identity(final_state,'final_state')
    return outputs, final_state

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_build_rnn(build_rnn)


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Build the Neural Network
Apply the functions you implemented above to:
- Apply embedding to input_data using your get_embed(input_data, vocab_size, embed_dim) function.
- Build RNN using cell and your build_rnn(cell, inputs) function.
- Apply a fully connected layer with a linear activation and vocab_size as the number of outputs.
Return the logits and final state in the following tuple (Logits, FinalState)
Can you write Python code for it?

def build_nn(cell, rnn_size, input_data, vocab_size, embed_dim):
    """
    Build part of the neural network
    :param cell: RNN cell
    :param rnn_size: Size of rnns
    :param input_data: Input data
    :param vocab_size: Vocabulary size
    :param embed_dim: Number of embedding dimensions
    :return: Tuple (Logits, FinalState)
    """
    # TODO: Implement Function
    inputs = get_embed(input_data, vocab_size, embed_dim)
    outputs, final_state = build_rnn(cell, inputs)
    logits = tf.contrib.layers.fully_connected(outputs,vocab_size,None)
    return logits, final_state

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_build_nn(build_nn)


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Check Point
This is your first checkpoint. If you ever decide to come back to this notebook or have to restart the notebook, you can start from here. The preprocessed data has been saved to disk.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import helper
import numpy as np
import problem_unittests as tests

int_text, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Checkpoint
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import tensorflow as tf
import numpy as np
import helper
import problem_unittests as tests

_, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()
seq_length, load_dir = helper.load_params()


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Explore the Data
Play around with view_sentence_range to view different parts of the data.
Can you write Python code for it?

view_sentence_range = (0, 10)

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

print('Dataset Stats')
print('Roughly the number of unique words: {}'.format(len({word: None for word in text.split()})))
scenes = text.split('\n\n')
print('Number of scenes: {}'.format(len(scenes)))
sentence_count_scene = [scene.count('\n') for scene in scenes]
print('Average number of sentences in each scene: {}'.format(np.average(sentence_count_scene)))

sentences = [sentence for scene in scenes for sentence in scene.split('\n')]
print('Number of lines: {}'.format(len(sentences)))
word_count_sentence = [len(sentence.split()) for sentence in sentences]
print('Average number of words in each line: {}'.format(np.average(word_count_sentence)))

print()
print('The sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Implement Generate Functions
Get Tensors
Get tensors from loaded_graph using the function get_tensor_by_name().  Get the tensors using the following names:
- "input:0"
- "initial_state:0"
- "final_state:0"
- "probs:0"
Return the tensors in the following tuple (InputTensor, InitialStateTensor, FinalStateTensor, ProbsTensor)
Can you write Python code for it?

def get_tensors(loaded_graph):
    """
    Get input, initial state, final state, and probabilities tensor from <loaded_graph>
    :param loaded_graph: TensorFlow graph loaded from file
    :return: Tuple (InputTensor, InitialStateTensor, FinalStateTensor, ProbsTensor)
    """
    # TODO: Implement Function
    tensorsSaved = (loaded_graph.get_tensor_by_name('input:0'),
                    loaded_graph.get_tensor_by_name('initial_state:0'),
                    loaded_graph.get_tensor_by_name('final_state:0'),
                    loaded_graph.get_tensor_by_name('probs:0'))
    return tensorsSaved

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_tensors(get_tensors)


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Input
Implement the get_inputs() function to create TF Placeholders for the Neural Network.  It should create the following placeholders:
- Input text placeholder named "input" using the TF Placeholder name parameter.
- Targets placeholder
- Learning Rate placeholder
Return the placeholders in the following tuple (Input, Targets, LearningRate)
Can you write Python code for it?

def get_inputs():
    """
    Create TF Placeholders for input, targets, and learning rate.
    :return: Tuple (input, targets, learning rate)
    """
    # TODO: Implement Function
    inputs = tf.placeholder(tf.int32,[None,None],name='input')
    targets = tf.placeholder(tf.int32,[None,None],name='targets')
    learning_rate = tf.placeholder(tf.float32,name='learning_rate')
    return inputs, targets, learning_rate

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_inputs(get_inputs)


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Neural Network Training
Hyperparameters
Tune the following parameters:

Set num_epochs to the number of epochs.
Set batch_size to the batch size.
Set rnn_size to the size of the RNNs.
Set embed_dim to the size of the embedding.
Set seq_length to the length of sequence.
Set learning_rate to the learning rate.
Set show_every_n_batches to the number of batches the neural network should print progress.
Can you write Python code for it?

# Number of Epochs
num_epochs = 100
# Batch Size
batch_size = 32
# RNN Size
rnn_size = 256
# Embedding Dimension Size
embed_dim = 299
# Sequence Length
seq_length = 10
# Learning Rate
learning_rate = 0.0016
# Show stats for every n number of batches
show_every_n_batches = 200
import time
"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
save_dir = './save'


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Preprocess all the data and save it
Running the code cell below will preprocess all the data and save it to file.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Preprocess Training, Validation, and Testing Data
helper.preprocess_and_save_data(data_dir, token_lookup, create_lookup_tables)


I am working on the file "Project3_tv_Script_Gen/dlnd_tv_script_generation.ipynb".
The first task is:
Train
Train the neural network on the preprocessed data.  If you have a hard time getting a good loss, check the forums to see if anyone is having the same problem.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
batches = get_batches(int_text, batch_size, seq_length)

with tf.Session(graph=train_graph) as sess:
    sess.run(tf.global_variables_initializer())

    lastBatchesTime = None
    for epoch_i in range(num_epochs):
        state = sess.run(initial_state, {input_text: batches[0][0]})

        for batch_i, (x, y) in enumerate(batches):
            feed = {
                input_text: x,
                targets: y,
                initial_state: state,
                lr: learning_rate}
            train_loss, state, _ = sess.run([cost, final_state, train_op], feed)

            # Show every <show_every_n_batches> batches
            if (epoch_i * len(batches) + batch_i) % show_every_n_batches == 0:
                print('Epoch {:>3} Batch {:>4}/{}   train_loss = {:.3f}'.format(
                    epoch_i,
                    batch_i,
                    len(batches),
                    train_loss))
                if lastBatchesTime == None:
                    lastBatchesTime = time.time()
                else:
                    print("time elaps:",str(time.time()-lastBatchesTime))
                    lastBatchesTime = time.time()

    # Save Model
    saver = tf.train.Saver()
    saver.save(sess, save_dir)
    print('Model Trained and Saved')


I am working on the file "Project4/Stroop effect.ipynb".
The first task is:
Firstly, assume the distribution of stimulus time for both congruent and incongruent lists are approximately normal, so we would be able to use Z-test or t-test. Secondly, we can see that we have a limited number of samples (24 and under 30), so we should use t-test instead of Z-test. Last but not least, the stimulus times are in pairs for both congruent and incongruent, and not independent events, so a pairwise t-test is our best bet.
Our goal is to determine whether the condition of the list would affect the stimulus time, or the mean stimulus time of each list would be significant different from each other. In other word, let's $\mu_D$, $\mu_I$ and $\mu_C$ be the mean difference between the two lists, the mean of the incongruent and the mean of the congruent, we have:
$$
H_0:\mu_D = \mu_I - \mu_C = 0
$$
$$
H_a:\mu_D = \mu_I - \mu_C \neq 0
$$
The null hypothesis is that there is no significant difference in the population average amount of time it takes to state the colors of the words in a congruent or incongruent condition. If $\mu_D$ is significantly different than 0, we can reject the null hypothesis and conclude that the stimulus time wouldn't be affected by the condition of the list. Otherwise, if $\mu_D$ is not diffent than zero then the condition of the list has no significant effect on the stimulus time. We use $\alpha = 0.05$ .
Before conducting the t-test, it would be beneficial for us to explore the dataset and its discriptive statistics.
Data exploration:
Descriptive statistics of stimulus time of the congruent list:
Can you write Python code for it?


I am working on the file "Project4/Stroop effect.ipynb".
The first task is:
Introduction
In a Stroop experiment, participants are shown two lists of word of color names, printed in different ink colors, and asked to read out loud the ink color that the word was printed. The two lists are the congruent one, where the ink colors of the words agree with the words, and the incongruent one, where the ink colors don't match with the words.
Hypothesis:
The independent variable in this case would be whether the list is a congruent or an incogruent one, while the dependent variable would be the time it takes for participants to name the ink colors.
Now looking at the dataset provided for the experiment:
Can you write Python code for it?

stroopdata = pd.read_csv("stroopdata.csv")
stroopdata


I am working on the file "Project4/Stroop effect.ipynb".
The first task is:
We can see that the incongruent group has higher mean, max and standard deviation comparing to the congruent list.
Now we can start plotting the data. The scatterplot of stimulus time of the congruent list in respective with the incongruent list shows a clear trend of the dataset that, in a pair, the incongurent stimulus time would always be higher than the congruent stimulus time.
Can you write Python code for it?

sns.lmplot(x = 'Congruent', y = 'Incongruent', data = stroopdata)
plt.title("Stimulus times of congruent and incongruent conditions")
plt.xlabel("Congruent list (sec)")
plt.ylabel("Incongruent list (sec)")
plt.show()

fig, ax = plt.subplots()
ax.hist([stroopdata['Congruent'], stroopdata['Incongruent']], label = ['Congruent', 'Incongruent'])
ax.legend()
plt.title('Histogram of stimulus time per condition')
plt.xlabel ("Stimulus time (sec)")
plt.ylabel("Frequency")
plt.show()


I am working on the file "Project4/Stroop effect.ipynb".
The first task is:
With the p-value is pvalue = 4.1003-08 < $\alpha = 0.05$, we can reject the null hypothesis.
Now consider the confidence interval:
Can you write Python code for it?

# Differences:
stroopdata['Difference'] = stroopdata['Incongruent'] - stroopdata['Congruent']
# Sum of all differences:
sumdiff = sum(stroopdata['Difference'])
# Std of all differences:
stddiff = np.std(stroopdata['Difference'])
# Average of all sum differences:
avgdiff = sumdiff/24
# CI = avgdiff +- std(diff)/sqrt(n)
lower_bound = avgdiff - 2.064*stddiff/np.sqrt(24)
upper_bound = avgdiff + 2.064*stddiff/np.sqrt(24)

print(lower_bound)
print(upper_bound)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Batch and pad the source and target sequences
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
def pad_sentence_batch(sentence_batch, pad_int):
    """Pad sentences with <PAD> so that each sentence of a batch has the same length"""
    max_sentence = max([len(sentence) for sentence in sentence_batch])
    return [sentence + [pad_int] * (max_sentence - len(sentence)) for sentence in sentence_batch]

def get_batches(sources, targets, batch_size, source_pad_int, target_pad_int):
    """Batch targets, sources, and the lengths of their sentences together"""
    for batch_i in range(0, len(sources)//batch_size):
        start_i = batch_i * batch_size

        # Slice the right amount for the batch
        sources_batch = sources[start_i:start_i + batch_size]
        targets_batch = targets[start_i:start_i + batch_size]

        # Pad
        pad_sources_batch = np.array(pad_sentence_batch(sources_batch, source_pad_int))
        pad_targets_batch = np.array(pad_sentence_batch(targets_batch, target_pad_int))

        # Need the lengths for the _lengths parameters
        pad_targets_lengths = []
        for target in pad_targets_batch:
            pad_targets_lengths.append(len(target))

        pad_source_lengths = []
        for source in pad_sources_batch:
            pad_source_lengths.append(len(source))

        yield pad_sources_batch, pad_targets_batch, pad_source_lengths, pad_targets_lengths


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Decoding Layer
Implement decoding_layer() to create a Decoder RNN layer.

Embed the target sequences
Construct the decoder LSTM cell (just like you constructed the encoder cell above)
Create an output layer to map the outputs of the decoder to the elements of our vocabulary
Use the your decoding_layer_train(encoder_state, dec_cell, dec_embed_input, target_sequence_length, max_target_sequence_length, output_layer, keep_prob) function to get the training logits.
Use your decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id, max_target_sequence_length, vocab_size, output_layer, batch_size, keep_prob) function to get the inference logits.

Note: You'll need to use tf.variable_scope to share variables between training and inference.
Can you write Python code for it?

def decoding_layer(dec_input, encoder_state,
                   target_sequence_length, max_target_sequence_length,
                   rnn_size,
                   num_layers, target_vocab_to_int, target_vocab_size,
                   batch_size, keep_prob, decoding_embedding_size):
    """
    Create decoding layer
    :param dec_input: Decoder input
    :param encoder_state: Encoder state
    :param target_sequence_length: The lengths of each sequence in the target batch
    :param max_target_sequence_length: Maximum length of target sequences
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :param target_vocab_size: Size of target vocabulary
    :param batch_size: The size of the batch
    :param keep_prob: Dropout keep probability
    :param decoding_embedding_size: Decoding embedding size
    :return: Tuple of (Training BasicDecoderOutput, Inference BasicDecoderOutput)
    """
    # TODO: Implement Function

#     embed = tf.contrib.layers.embed_sequence(dec_input, target_vocab_size, decoding_embedding_size)

    dec_embeddings = tf.Variable(tf.random_uniform([target_vocab_size, decoding_embedding_size], -1, 1))
    dec_embed_input = tf.nn.embedding_lookup(dec_embeddings, dec_input)

    def lstm_cell():
        lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
        return tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)

    # Stack up multiple LSTM layers, for deep learning
    dec_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(num_layers)])

    output_layer = Dense(target_vocab_size,
                         kernel_initializer=tf.truncated_normal_initializer(mean=0.0,
                                                                            stddev=0.1))

#     outputs, final_state = tf.nn.dynamic_rnn(dec_cell, dec_embed_input, sequence_length=target_sequence_length,dtype=tf.float32)
#     output_layer = tf.contrib.layers.fully_connected(outputs,...


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Graph
Build the graph using the neural network you implemented.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
save_path = 'checkpoints/dev'
(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()
max_target_sentence_length = max([len(sentence) for sentence in source_int_text])

train_graph = tf.Graph()
with train_graph.as_default():
    input_data, targets, lr, keep_prob, target_sequence_length, max_target_sequence_length, source_sequence_length = model_inputs()

    #sequence_length = tf.placeholder_with_default(max_target_sentence_length, None, name='sequence_length')
    input_shape = tf.shape(input_data)

    train_logits, inference_logits = seq2seq_model(tf.reverse(input_data, [-1]),
                                                   targets,
                                                   keep_prob,
                                                   batch_size,
                                                   source_sequence_length,
                                                   target_sequence_length,
                                                   max_target_sequence_length,
                                                   len(source_vocab_to_int),
                                                   len(target_vocab_to_int),
                                                   encoding_embedding_size,
                                                   decoding_embedding_size,
                                                   rnn_size,
                                                   num_layers,
                                                   target_vocab_to_int)

    training_logits = tf.identity(train_logits.rnn_output, name='logits')
    inference_logits = tf.identity(inference_logits.sample_id, name='predictions')

    masks = tf.sequence_mask(target_sequence_length, max_target_sequence_length, dtype=tf.float32, name='masks')

    with tf.name_scope("optimization"):
        # Loss function
        cost = tf.contrib.seq2seq.sequence_loss(
            training_logits,
            targets,
       ...


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Neural Network
Apply the functions you implemented above to:

Encode the input using your encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob,  source_sequence_length, source_vocab_size, encoding_embedding_size).
Process target data using your process_decoder_input(target_data, target_vocab_to_int, batch_size) function.
Decode the encoded input using your decoding_layer(dec_input, enc_state, target_sequence_length, max_target_sentence_length, rnn_size, num_layers, target_vocab_to_int, target_vocab_size, batch_size, keep_prob, dec_embedding_size) function.
Can you write Python code for it?

def seq2seq_model(input_data, target_data, keep_prob, batch_size,
                  source_sequence_length, target_sequence_length,
                  max_target_sentence_length,
                  source_vocab_size, target_vocab_size,
                  enc_embedding_size, dec_embedding_size,
                  rnn_size, num_layers, target_vocab_to_int):
    """
    Build the Sequence-to-Sequence part of the neural network
    :param input_data: Input placeholder
    :param target_data: Target placeholder
    :param keep_prob: Dropout keep probability placeholder
    :param batch_size: Batch Size
    :param source_sequence_length: Sequence Lengths of source sequences in the batch
    :param target_sequence_length: Sequence Lengths of target sequences in the batch
    :param source_vocab_size: Source vocabulary size
    :param target_vocab_size: Target vocabulary size
    :param enc_embedding_size: Decoder embedding size
    :param dec_embedding_size: Encoder embedding size
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :return: Tuple of (Training BasicDecoderOutput, Inference BasicDecoderOutput)
    """
    # TODO: Implement Function

    encode_output, encode_state = encoding_layer(input_data, rnn_size, num_layers, keep_prob,
                                                 source_sequence_length, source_vocab_size,
                                                 enc_embedding_size)

    decoder_input = process_decoder_input(target_data, target_vocab_to_int, batch_size)

    training_logits, inference_logits = decoding_layer(decoder_input, encode_state,
                                                       target_sequence_length,
                                                       max_target_sentence_length,
                                                       rnn_size, num_layers,
                                                       target_vocab_to_int,
                     ...


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Neural Network
You'll build the components necessary to build a Sequence-to-Sequence model by implementing the following functions below:
- model_inputs
- process_decoder_input
- encoding_layer
- decoding_layer_train
- decoding_layer_infer
- decoding_layer
- seq2seq_model
Input
Implement the model_inputs() function to create TF Placeholders for the Neural Network. It should create the following placeholders:

Input text placeholder named "input" using the TF Placeholder name parameter with rank 2.
Targets placeholder with rank 2.
Learning rate placeholder with rank 0.
Keep probability placeholder named "keep_prob" using the TF Placeholder name parameter with rank 0.
Target sequence length placeholder named "target_sequence_length" with rank 1
Max target sequence length tensor named "max_target_len" getting its value from applying tf.reduce_max on the target_sequence_length placeholder. Rank 0.
Source sequence length placeholder named "source_sequence_length" with rank 1

Return the placeholders in the following the tuple (input, targets, learning rate, keep probability, target sequence length, max target sequence length, source sequence length)
Can you write Python code for it?

def model_inputs():
    """
    Create TF Placeholders for input, targets, learning rate, and lengths of source and target sequences.
    :return: Tuple (input, targets, learning rate, keep probability, target sequence length,
    max target sequence length, source sequence length)
    """
    # TODO: Implement Function
    inputs_ = tf.placeholder(tf.int32, [None, None], name="input")
    targets_ = tf.placeholder(tf.int32, [None, None], name="target")
    learning_rate = tf.placeholder(tf.float32, name="learning_rate")
    keep_probability = tf.placeholder(tf.float32, name="keep_prob")
    target_sequence_length = tf.placeholder(tf.int32, [None], name="target_sequence_length")
    max_target_length = tf.reduce_max(target_sequence_length, name='max_target_len')
    source_sequence_length = tf.placeholder(tf.int32, [None], name='source_sequence_length')

    return inputs_, targets_, learning_rate, keep_probability, target_sequence_length, max_target_length, source_sequence_length

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_inputs(model_inputs)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Check Point
This is your first checkpoint. If you ever decide to come back to this notebook or have to restart the notebook, you can start from here. The preprocessed data has been saved to disk.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np
import helper

(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Check the Version of TensorFlow and Access to GPU
This will check to make sure you have the correct version of TensorFlow and access to a GPU
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf
from tensorflow.python.layers.core import Dense

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.1'), 'Please use TensorFlow version 1.1 or newer'
print('TensorFlow Version: {}'.format(tf.__version__))

# Check for a GPU
if not tf.test.gpu_device_name():
    warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Checkpoint
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import tensorflow as tf
import numpy as np
import helper
import problem_unittests as tests

_, (source_vocab_to_int, target_vocab_to_int), (source_int_to_vocab, target_int_to_vocab) = helper.load_preprocess()
load_path = helper.load_params()


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Decoding - Inference
Create inference decoder:
* Create a tf.contrib.seq2seq.GreedyEmbeddingHelper
* Create a tf.contrib.seq2seq.BasicDecoder
* Obtain the decoder outputs from tf.contrib.seq2seq.dynamic_decode
Can you write Python code for it?

def decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id,
                         end_of_sequence_id, max_target_sequence_length,
                         vocab_size, output_layer, batch_size, keep_prob):
    """
    Create a decoding layer for inference
    :param encoder_state: Encoder state
    :param dec_cell: Decoder RNN Cell
    :param dec_embeddings: Decoder embeddings
    :param start_of_sequence_id: GO ID
    :param end_of_sequence_id: EOS Id
    :param max_target_sequence_length: Maximum length of target sequences
    :param vocab_size: Size of decoder/target vocabulary
    :param decoding_scope: TenorFlow Variable Scope for decoding
    :param output_layer: Function to apply the output layer
    :param batch_size: Batch size
    :param keep_prob: Dropout keep probability
    :return: BasicDecoderOutput containing inference logits and sample_id
    """
    # TODO: Implement Function

    start_tokens = tf.fill([batch_size], start_of_sequence_id)

    inference_helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(embedding=dec_embeddings,
                                                                start_tokens=start_tokens,
                                                                end_token=end_of_sequence_id)

    inference_decoder = tf.contrib.seq2seq.BasicDecoder(cell=dec_cell, helper=inference_helper,
                                                        initial_state=encoder_state, output_layer=output_layer)

    outputs, _ = tf.contrib.seq2seq.dynamic_decode(inference_decoder, impute_finished = True,
                                                   maximum_iterations=max_target_sequence_length)

    return outputs

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer_infer(decoding_layer_infer)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Decoding - Training
Create a training decoding layer:
* Create a tf.contrib.seq2seq.TrainingHelper
* Create a tf.contrib.seq2seq.BasicDecoder
* Obtain the decoder outputs from tf.contrib.seq2seq.dynamic_decode
Can you write Python code for it?

def decoding_layer_train(encoder_state, dec_cell, dec_embed_input,
                         target_sequence_length, max_summary_length,
                         output_layer, keep_prob):
    """
    Create a decoding layer for training
    :param encoder_state: Encoder State
    :param dec_cell: Decoder RNN Cell
    :param dec_embed_input: Decoder embedded input
    :param target_sequence_length: The lengths of each sequence in the target batch
    :param max_summary_length: The length of the longest sequence in the batch
    :param output_layer: Function to apply the output layer
    :param keep_prob: Dropout keep probability
    :return: BasicDecoderOutput containing training logits and sample_id
    """
    # TODO: Implement Function

    training_helper = tf.contrib.seq2seq.TrainingHelper(inputs=dec_embed_input,
                                                        sequence_length=target_sequence_length,
                                                        name = "training_helper")

    basic_decode = tf.contrib.seq2seq.BasicDecoder(cell=dec_cell,
                                                   helper=training_helper,
                                                   initial_state=encoder_state,
                                                   output_layer=output_layer)

    decoder_output,_ = tf.contrib.seq2seq.dynamic_decode(decoder=basic_decode,
                                                         maximum_iterations=max_summary_length)

    return decoder_output

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer_train(decoding_layer_train)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Implement Preprocessing Function
Text to Word Ids
As you did with other RNNs, you must turn the text into a number so the computer can understand it. In the function text_to_ids(), you'll turn source_text and target_text from words to ids.  However, you need to add the <EOS> word id at the end of target_text.  This will help the neural network predict when the sentence should end.
You can get the <EOS> word id by doing:
python
target_vocab_to_int['<EOS>']
You can get other word ids using source_vocab_to_int and target_vocab_to_int.
Can you write Python code for it?

def text_to_ids(source_text, target_text, source_vocab_to_int, target_vocab_to_int):
    """
    Convert source and target text to proper word ids
    :param source_text: String that contains all the source text.
    :param target_text: String that contains all the target text.
    :param source_vocab_to_int: Dictionary to go from the source words to an id
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :return: A tuple of lists (source_id_text, target_id_text)
    """
    # TODO: Implement Function
    source_id_text = [[source_vocab_to_int[word] for word in sentence.split()] for sentence in source_text.split("\n")]
    target_id_text = [[target_vocab_to_int[word] for word in sentence.split()]  + [target_vocab_to_int['<EOS>']] for sentence in target_text.split("\n")]

    return source_id_text, target_id_text

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_text_to_ids(text_to_ids)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Neural Network Training
Hyperparameters
Tune the following parameters:

Set epochs to the number of epochs.
Set batch_size to the batch size.
Set rnn_size to the size of the RNNs.
Set num_layers to the number of layers.
Set encoding_embedding_size to the size of the embedding for the encoder.
Set decoding_embedding_size to the size of the embedding for the decoder.
Set learning_rate to the learning rate.
Set keep_probability to the Dropout keep probability
Set display_step to state how many steps between each debug output statement
Can you write Python code for it?

# Number of Epochs
epochs = 5
# Batch Size
batch_size = 128
# RNN Size
rnn_size = 128
# Number of Layers
num_layers = 2
# Embedding Size
encoding_embedding_size = 128
decoding_embedding_size = 128
# Learning Rate
learning_rate = 0.01
# Dropout Keep Probability
keep_probability = 0.8
display_step = 100


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Preprocess all the data and save it
Running the code cell below will preprocess all the data and save it to file.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
helper.preprocess_and_save_data(source_path, target_path, text_to_ids)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Save Parameters
Save the batch_size and save_path parameters for inference.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Save parameters for checkpoint
helper.save_params(save_path)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Sentence to Sequence
To feed a sentence into the model for translation, you first need to preprocess it.  Implement the function sentence_to_seq() to preprocess new sentences.

Convert the sentence to lowercase
Convert words into ids using vocab_to_int
Convert words not in the vocabulary, to the <UNK> word id.
Can you write Python code for it?

def sentence_to_seq(sentence, vocab_to_int):
    """
    Convert a sentence to a sequence of ids
    :param sentence: String
    :param vocab_to_int: Dictionary to go from the words to an id
    :return: List of word ids
    """
    # TODO: Implement Function

    return [vocab_to_int[word] if word in vocab_to_int.keys() else vocab_to_int['<UNK>'] for word in sentence.split()]

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_sentence_to_seq(sentence_to_seq)


I am working on the file "Project4/language-translation/dlnd_language_translation.ipynb".
The first task is:
Translate
This will translate translate_sentence from English to French.
Can you write Python code for it?

translate_sentence = 'he saw a old yellow truck .'

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
translate_sentence = sentence_to_seq(translate_sentence, source_vocab_to_int)

loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
    # Load saved model
    loader = tf.train.import_meta_graph(load_path + '.meta')
    loader.restore(sess, load_path)

    input_data = loaded_graph.get_tensor_by_name('input:0')
    logits = loaded_graph.get_tensor_by_name('predictions:0')
    target_sequence_length = loaded_graph.get_tensor_by_name('target_sequence_length:0')
    source_sequence_length = loaded_graph.get_tensor_by_name('source_sequence_length:0')
    keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0')

    translate_logits = sess.run(logits, {input_data: [translate_sentence]*batch_size,
                                         target_sequence_length: [len(translate_sentence)*2]*batch_size,
                                         source_sequence_length: [len(translate_sentence)]*batch_size,
                                         keep_prob: 1.0})[0]

print('Input')
print('  Word Ids:      {}'.format([i for i in translate_sentence]))
print('  English Words: {}'.format([source_int_to_vocab[i] for i in translate_sentence]))

print('\nPrediction')
print('  Word Ids:      {}'.format([i for i in translate_logits]))
print('  French Words: {}'.format(" ".join([target_int_to_vocab[i] for i in translate_logits])))


I am working on the file "Project5-digit-recognition/digit_recognition.ipynb".
The first task is:
可视化 mnist
我们可以通过 matplotlib 来可视化我们的原始数据集。
Can you write Python code for it?

import matplotlib.pyplot as plt
import random

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

for i in range(15):
    plt.subplot(3, 5, i+1)
    index = random.randint(0, n_train-1)
    plt.title(str(y_raw[index]))
    plt.imshow(X_raw[index], cmap='gray')
    plt.axis('off')


I am working on the file "Project5-digit-recognition/digit_recognition.ipynb".
The first task is:
问题 2
你为解决这个问题采取了什么技术？请详细介绍你使用的技术。
回答：
1. 我的模型构造上，采用了卷积神经网络作为训练，最后一层全连接层作为分类输出
2. 为何使用卷积神经网络：首先这是图像识别方面的，如果是使用全连接的深度神经网络，这需要大量的权重进行训练，耗时而且容易过拟合。
卷积神经网络通过使用卷积核这个方法，让同一个卷积核内的图像共享权重，这样不仅减少了权重的数量，而且容易在每一层生成图像的某种模式如轮廓等
3. 为何使用max pool：我们可以使用增大stride来增大卷积核移动的步伐从而减少feature map，但是这样会丢失很多信息。在保持小的stride的前提下可以使用maxpool的技术进行优化，一样能减少feature map
4. 为何使用dropout：使用dropout，在前一层到后面一层之间(这里是训练的最后一层卷积和全连接层的输入)随机选择一部分设置为0，在实践中这种方法很好的防止过拟合
可视化你的网络模型
参考链接：visualization
可以是 PNG 格式，也可以是 SVG 格式。
Can you write Python code for it?

from keras.utils.visualize_util import plot, model_to_dot
from IPython.display import Image, SVG
# 可视化网络模型
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))


I am working on the file "Project5-digit-recognition/digit_recognition.ipynb".
The first task is:
问题 3
你最终的模型架构是什么样的？（什么类型的模型，层数，大小, 如何连接等）
回答：
1. 使用了卷积神经网络结合全连接的神经网络
2. 层数总共10层。4层卷积，maxpool 2层，input,dropout,output和flaten各一层
3. 连接顺序是input-cnn-maxpool-cnn-maxpool-cnn-cnn-dropout-flaten。然后分别5个1层的dense层，连接着flaten层
练习：训练你的网络模型
你需要设置20%的数据作为验证集，以保证模型没有过拟合。
练习：计算你的模型准确率
我们刚才得到了模型每个数字的准确率，现在让我们来计算整体准确率，按照完全预测正确数字序列的标准来计算。
比如 1,2,3,10,10 预测成了 1,2,10,10,10 算错，而不是算对了80%。
Can you write Python code for it?

# 测试模型
prediction = model.predict(X_test)

# 计算测试集中数字序列的准确率
def evaluate(model):
    right = 0
    error = 0
    for i in range (1000):
        right_num = 0
        for j in range(5):
            y_pred_num = np.argmax(prediction[j][i])
            y_test_num = np.argmax(y_test[j][i])
            if y_pred_num == y_test_num:
                right_num += 1
        if right_num == 5:
            right += 1
        else:
            error += 1

    return float(right)/(right+error)

evaluate(model)


I am working on the file "ProjectEuler/ProjectEuler001.ipynb".
The first task is:
Problem 3 - Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Can you write Python code for it?

def check_divisor(n, i):
    while not n % i:
        n //= i
    return n

def largest_prime_factor(n):
    n = check_divisor(n, 2)
    if n == 1:
        return 2
    i = 3
    while i <= math.sqrt(n):
        n = check_divisor(n, i)
        i += 2
    if n > 2:
        return n
    return i - 2

print(largest_prime_factor(600851475143))


I am working on the file "ProjectEuler/ProjectEuler001.ipynb".
The first task is:
Problem 5 - Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Can you write Python code for it?

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

def smallest_multiple(n):
    s_m = 1
    for num in range(1, n + 1):
        s_m = lcm(s_m, num)
    return s_m

print(smallest_multiple(20))


I am working on the file "ProjectEuler/ProjectEuler001.ipynb".
The first task is:
Problem 6 - Sum square difference
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Sol
1^2 + 2^2 + ... + n^2 = n * (n + 1) * (2 * n + 1) / 6
1 + 2 + ... + n = n * (n + 1) /2
difference = n * (n + 1) * (n - 1) * (3 * n + 2) / 12
Can you write Python code for it?

def sum_square_diff(n):
    return n * (n + 1) * (n - 1) * (3 * n + 2) // 12

print(sum_square_diff(100))


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance BM25.ipynb".
The first task is:
'sheer' and 'courtain' are present and that's all
How many search terms are not present in description and title for ranked 3 documents
Ranked 3 documents are the most relevents searches, but how many search queries doesn't include the searched term in the description and the title
Can you write Python code for it?

train['search_term_word_count'] = gl.text_analytics.count_words(train['search_term'])
ranked3doc = train[train['relevance'] == 3]
print ranked3doc.head()
len(ranked3doc)

words_search = gl.text_analytics.tokenize(ranked3doc['search_term'], to_lower = True)
words_description = gl.text_analytics.tokenize(ranked3doc['product_description'], to_lower = True)
words_title = gl.text_analytics.tokenize(ranked3doc['product_title'], to_lower = True)
wordsdiff_desc = []
wordsdiff_title = []
puid = []
search_term = []
ws_count = []
ws_count_used_desc = []
ws_count_used_title = []
for item in xrange(len(ranked3doc)):
    ws = words_search[item]
    pd = words_description[item]
    pt = words_title[item]
    diff = set(ws) - set(pd)
    if diff is None:
        diff = 0
    wordsdiff_desc.append(diff)

    diff2 = set(ws) - set(pt)
    if diff2 is None:
        diff2 = 0
    wordsdiff_title.append(diff2)

    puid.append(ranked3doc[item]['product_uid'])
    search_term.append(ranked3doc[item]['search_term'])
    ws_count.append(len(ws))
    ws_count_used_desc.append(len(ws) - len(diff))
    ws_count_used_title.append(len(ws) - len(diff2))

differences = gl.SFrame({"puid" : puid,
                         "search term": search_term,
                         "diff desc" : wordsdiff_desc,
                         "diff title" : wordsdiff_title,
                         "ws count" : ws_count,
                         "ws count used desc" : ws_count_used_desc,
                         "ws count used title" : ws_count_used_title})

differences.sort(['ws count used desc', 'ws count used title'])

print "No terms used in description : " + str(len(differences[differences['ws count used desc'] == 0]))
print "No terms used in title : " + str(len(differences[differences['ws count used title'] == 0]))
print "No terms used in description and title : " + str(len(differences[(differences['ws count used desc'] == 0) &
                                                                        (differences['ws count used title'] ==...


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance BM25.ipynb".
The first task is:
Let's explore some data
Let's examine 3 different queries and products:
* first from the training set
* somewhere in the moddle in the training set
* the last one from the training set
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance BM25.ipynb".
The first task is:
Load data from CSV files
Can you write Python code for it?

train = gl.SFrame.read_csv("../data/train.csv")

test = gl.SFrame.read_csv("../data/test.csv")

desc = gl.SFrame.read_csv("../data/product_descriptions.csv")


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance BM25.ipynb".
The first task is:
Stemming
Can you write Python code for it?

#stemmer = SnowballStemmer("english")
stemmer = PorterStemmer()
def stem(word):
    singles = [stemmer.stem(plural) for plural in unicode(word, errors='replace').split()]
    text = ' '.join(singles)
    return text

print "Starting stemming train search term..."
stemmed = train['search_term'].apply(stem)
train['stem_search_term'] = stemmed

print "Starting stemming train product description..."
stemmed = train['product_description'].apply(stem)
train['stem_product_description'] = stemmed

print "Starting stemming train product title..."
stemmed = train['product_title'].apply(stem)
train['stem_product_title'] = stemmed

print "Starting stemming test search term..."
stemmed = test['search_term'].apply(stem)
test['stem_search_term'] = stemmed

print "Starting stemming test product description..."
stemmed = test['product_description'].apply(stem)
test['stem_product_description'] = stemmed

print "Starting stemming test product title..."
stemmed = test['product_title'].apply(stem)
test['stem_product_title'] = stemmed


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance BM25.ipynb".
The first task is:
only 'wood' is present from search term
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Features.ipynb".
The first task is:
Data merging and feature engineering
Can you write Python code for it?

# merge train with description
train = train.join(desc, on = 'product_uid', how = 'left')

# merge test with description
test = test.join(desc, on = 'product_uid', how = 'left')

# if some attributes has no so we don't need them
print len(attr)
attr = attr[attr['value'] != "No"]
print len(attr)

# if some attributes has "yes" we compy the value so we can search in it
attr['value'] = attr.apply(lambda x: x['name'] if x['value'] == "Yes" else x['value'])


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Features.ipynb".
The first task is:
Let's select brands
Can you write Python code for it?

brands = attr[attr['name'] == "MFG Brand Name"]

brands.head()


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Features.ipynb".
The first task is:
TF-IDF with linear regression
Can you write Python code for it?

def calculateTfIdf(cols, data, searchColTfIdfName):
    for item in xrange(len(cols)):
        colName = cols[item]
        newColNameWordCount = colName + "_word_count"
        newColNameTfIdf = colName + "_tfidf"
        newColDistance = colName + "_distance"

        wordCount = gl.text_analytics.count_words(data[colName])
        data[newColNameWordCount] = wordCount

        tfidf = gl.text_analytics.tf_idf(data[newColNameWordCount])
        data[newColNameTfIdf] = tfidf
        #print colName
        if searchColTfIdfName != colName:
            data[newColDistance] = data.apply(lambda x: 0 if x[newColNameTfIdf] is None else gl.distances.cosine(x[searchColTfIdfName],x[newColNameTfIdf]))

    return data

# columns = ['search_term', 'product_title', 'product_description', 'value', 'bullets.Bullet01',
#          'bullets.Bullet02', 'bullets.Bullet03', 'bullets.Bullet04', 'bullets.Bullet05', 'bullets.Bullet06'
#          , 'bullets.Bullet07', 'bullets.Bullet08', 'bullets.Bullet09', 'bullets.Bullet10', 'bullets.Bullet11'
#          , 'bullets.Bullet12', 'bullets.Bullet13', 'bullets.Bullet14', 'bullets.Bullet15', 'bullets.Bullet16'
#          , 'bullets.Bullet17', 'bullets.Bullet18', 'bullets.Bullet19', 'bullets.Bullet20', 'bullets.Bullet21'
#          , 'bullets.Bullet22']

columns = ['search_term', 'product_title', 'product_description', 'value']

train = calculateTfIdf(columns, train, 'search_term_tfidf')

test = calculateTfIdf(columns, test, 'search_term_tfidf')

featuresDistance = [s for s in train.column_names() if "distance" in s]
print featuresDistance

#train = train.dropna('value_distance')

model1 = gl.linear_regression.create(train, target = 'relevance', features = featuresDistance)

#let's take a look at the weights before we plot
model1.get("coefficients")

'''
predictions_test = model1.predict(test)
test_errors = predictions_test - test['relevance']
RSS_test = sum(test_errors * test_errors)
print RSS_test
'''

predictions_test =...


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Polynomial.ipynb".
The first task is:
'angle bracket' search term is not contained in the body. 'angle' would be after stemming however 'bracket' is not.
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Polynomial.ipynb".
The first task is:
'sheer' and 'courtain' are present and that's all
How many search terms are not present in description and title for ranked 3 documents
Ranked 3 documents are the most relevents searches, but how many search queries doesn't include the searched term in the description and the title
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Polynomial.ipynb".
The first task is:
Data merging
Can you write Python code for it?

# merge train with description
train = train.join(desc, on = 'product_uid', how = 'left')

# merge test with description
test = test.join(desc, on = 'product_uid', how = 'left')


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Polynomial.ipynb".
The first task is:
Load data from CSV files
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance Polynomial.ipynb".
The first task is:
Stemming
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance RF Reg.ipynb".
The first task is:
Data merging
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance RF Reg.ipynb".
The first task is:
Let's explore some data
Let's examine 3 different queries and products:
* first from the training set
* somewhere in the moddle in the training set
* the last one from the training set
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance RF Reg.ipynb".
The first task is:
Stemming
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance RF Reg.ipynb".
The first task is:
TF-IDF with linear regression
Can you write Python code for it?

train['search_term_word_count'] = gl.text_analytics.count_words(train['stem_search_term'])
train_search_tfidf = gl.text_analytics.tf_idf(train['search_term_word_count'])

train['search_tfidf'] = train_search_tfidf

train['product_desc_word_count'] = gl.text_analytics.count_words(train['stem_product_description'])
train_desc_tfidf = gl.text_analytics.tf_idf(train['product_desc_word_count'])

train['desc_tfidf'] = train_desc_tfidf

train['product_title_word_count'] = gl.text_analytics.count_words(train['stem_product_title'])
train_title_tfidf = gl.text_analytics.tf_idf(train['product_title_word_count'])
train['title_tfidf'] = train_title_tfidf

train['distance_desc'] = train.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['desc_tfidf']))
#train['distance_desc_sqrt'] = train['distance_desc'] ** 2
train['distance_title'] = train.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['title_tfidf']))
#train['distance_title_sqrt'] = train['distance_title'] ** 3

model1 = gl.random_forest_regression.create(train, target = 'relevance',
                                             features = ['distance_desc', 'distance_title'],
                                             num_trees = 500,
                                             validation_set = None)

test['search_term_word_count'] = gl.text_analytics.count_words(test['stem_search_term'])
test_search_tfidf = gl.text_analytics.tf_idf(test['search_term_word_count'])
test['search_tfidf'] = test_search_tfidf
test['product_desc_word_count'] = gl.text_analytics.count_words(test['stem_product_description'])
test_desc_tfidf = gl.text_analytics.tf_idf(test['product_desc_word_count'])
test['desc_tfidf'] = test_desc_tfidf
test['product_title_word_count'] = gl.text_analytics.count_words(test['stem_product_title'])
test_title_tfidf = gl.text_analytics.tf_idf(test['product_title_word_count'])
test['title_tfidf'] = test_title_tfidf

test['distance_desc'] = test.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['desc_tfidf']))
#test['distance_desc_sqrt'] =...


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance RF Reg.ipynb".
The first task is:
only 'wood' is present from search term
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance TF-IDF.ipynb".
The first task is:
'angle bracket' search term is not contained in the body. 'angle' would be after stemming however 'bracket' is not.
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance TF-IDF.ipynb".
The first task is:
'sheer' and 'courtain' are present and that's all
How many search terms are not present in description and title for ranked 3 documents
Ranked 3 documents are the most relevents searches, but how many search queries doesn't include the searched term in the description and the title
Can you write Python code for it?


I am working on the file "ProjectSearchRelevance.Python/Home Depot Product Search Relevance TF-IDF.ipynb".
The first task is:
TF-IDF with linear regression
Can you write Python code for it?

train_search_tfidf = gl.text_analytics.tf_idf(train['search_term_word_count'])

train['search_tfidf'] = train_search_tfidf

train['product_desc_word_count'] = gl.text_analytics.count_words(train['product_description'])
train_desc_tfidf = gl.text_analytics.tf_idf(train['product_desc_word_count'])

train['desc_tfidf'] = train_desc_tfidf

train['product_title_word_count'] = gl.text_analytics.count_words(train['product_title'])
train_title_tfidf = gl.text_analytics.tf_idf(train['product_title_word_count'])
train['title_tfidf'] = train_title_tfidf

train['distance'] = train.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['desc_tfidf']))
train['distance2'] = train.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['title_tfidf']))

model1 = gl.linear_regression.create(train, target = 'relevance', features = ['distance', 'distance2'], validation_set = None)

#let's take a look at the weights before we plot
model1.get("coefficients")

test['search_term_word_count'] = gl.text_analytics.count_words(test['search_term'])
test_search_tfidf = gl.text_analytics.tf_idf(test['search_term_word_count'])
test['search_tfidf'] = test_search_tfidf
test['product_desc_word_count'] = gl.text_analytics.count_words(test['product_description'])
test_desc_tfidf = gl.text_analytics.tf_idf(test['product_desc_word_count'])
test['desc_tfidf'] = test_desc_tfidf
test['product_title_word_count'] = gl.text_analytics.count_words(test['product_title'])
test_title_tfidf = gl.text_analytics.tf_idf(test['product_title_word_count'])
test['title_tfidf'] = test_title_tfidf
test['distance'] = test.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['desc_tfidf']))
test['distance2'] = test.apply(lambda x: gl.distances.cosine(x['search_tfidf'],x['title_tfidf']))

'''
predictions_test = model1.predict(test)
test_errors = predictions_test - test['relevance']
RSS_test = sum(test_errors * test_errors)
print RSS_test
'''

output

submission = gl.SFrame(test['id'])

submission.add_column(output)
submission.rename({'X1': 'id',...


I am working on the file "Project_2.ipynb".
The first task is:
As we can see, the Average Ratings have been computed and now applied to the original DataFrame Object, here is the top 5 rows.
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Below I've creating a series of the data i wish to analyze further and do computational processes on.
Can you write Python code for it?

games_df.set_index(['Round1', 'Round2', 'Round3', 'Round4', 'Round5', 'Round6', 'Round7'])['Ratings']


I am working on the file "Project_2.ipynb".
The first task is:
Creating an object that will process the parse_series function and return its list values.
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Here I wanted to create a function that would loop through the data and return the desired result.
This function is indexing the cells in the columns to their respective indexed players rating and replacing them, after this replacement is made all the data is then computed and divided by the number of games played which returns a column with the players Average Pre Tournament Chess Rating of Opponents.
Can you write Python code for it?

def parse_series(series):
    player_mapping = {}
    average_rating = []
    for index, row in series.iterrows():
        player_mapping[int(row['Player_ID'])] = int(row['Ratings'])
    for index, row in series.iterrows():

        if row[0] is not None:
            try:
                key1 = int(row.Round1)
                row.Round1 = player_mapping[key1]

                key2 = int(row.Round2)
                row.Round2 = player_mapping[key2]

                key3 = int(row.Round3)
                row.Round3 = player_mapping[key3]

                key4 = int(row.Round4)
                row.Round4 = player_mapping[key4]

                key5 = int(row.Round5)
                row.Round5 = player_mapping[key5]

                key6 = int(row.Round6)
                row.Round6 = player_mapping[key6]

                key7 = int(row.Round7)
                row.Round7 = player_mapping[key7]

                total = row.Round1 + row.Round2 + row.Round3 + row.Round4 + row.Round5 + row.Round6 + row.Round7
                games = len([row.Round1, row.Round2, row.Round3, row.Round4, row.Round5, row.Round6, row.Round7])
                average_rating.append(total/games)
            except KeyError:
                total = int(row.Round1) + int(row.Round2) + int(row.Round3) + int(row.Round4) + int(row.Round5) + int(row.Round6) + int(row.Round7)
                games = len([row.Round1, row.Round2, row.Round3, row.Round4, row.Round5, row.Round6, row.Round7])
                average_rating.append(total/games)
    return average_rating


I am working on the file "Project_2.ipynb".
The first task is:
Here are te last 5 rows in the DataFrame object.
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
I will de doing the same with the alternate rows and cleaning it in the process.
Can you write Python code for it?

for item in id_data:
    uscf_id.append(item.replace('R', '').replace('P', ' ')[:8])
    ratings.append(item.replace('R', '').replace('P', ' ')[11:16])


I am working on the file "Project_2.ipynb".
The first task is:
Now since all computations are done, and the data that we want is in the DataFrame, we can create and send to CSV File using the to_csv method.
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Print out the list, to inspect how it's formated and changes that will need to be made. (Cleaning)
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Printing the DataFrame constantly throughout helps to visualize the changes and not make unnecessary changes to the object.
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Reading the text file into the Ipython Notebook
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Using Regex to clean some of the data inside the table before we loop through it, so it can be easier to accesss the desired information from the table list we've created.
Can you write Python code for it?


I am working on the file "Project_2.ipynb".
The first task is:
Using a for-loop to parse through the table, i've split the rows seperately becaues they are formated differently, making it easier to work once on each unique row set.
Can you write Python code for it?

for line in text_table:
    if not re.search(dash, line):
        if re.search(state, line) and not re.search(dash, line):
            text =  line.replace('/', '').replace('-','').replace('>','').replace(':','')
            state_num = text.strip().replace('|', ',')[:3].strip(',')
            if re.search(state, state_num):
                player_state.append(state_num)
                id_data.append(text[5:].strip('').replace('|',','))
            elif re.search(number, state_num):
                name_data.append(text[4:].strip().replace('|',','))
                player_number.append(state_num)


I am working on the file "Project_2_image_classification/dlnd_image_classification.ipynb".
The first task is:
Build the network
For the neural network, you'll build each layer into a function.  Most of the code you've seen has been outside of functions. To test your code more thoroughly, we require that you put each layer in a function.  This allows us to give you better feedback and test for simple mistakes using our unittests before you submit your project.

Note: If you're finding it hard to dedicate enough time for this course each week, we've provided a small shortcut to this part of the project. In the next couple of problems, you'll have the option to use classes from the TensorFlow Layers or TensorFlow Layers (contrib) packages to build each layer, except the layers you build in the "Convolutional and Max Pooling Layer" section.  TF Layers is similar to Keras's and TFLearn's abstraction to layers, so it's easy to pickup.
However, if you would like to get the most out of this course, try to solve all the problems without using anything from the TF Layers packages. You can still use classes from other packages that happen to have the same name as ones you find in TF Layers! For example, instead of using the TF Layers version of the conv2d class, tf.layers.conv2d, you would want to use the TF Neural Network version of conv2d, tf.nn.conv2d.

Let's begin!
Input
The neural network needs to read the image data, one-hot encoded labels, and dropout keep probability. Implement the following functions
* Implement neural_net_image_input
 * Return a TF Placeholder
 * Set the shape using image_shape with batch size set to None.
 * Name the TensorFlow placeholder "x" using the TensorFlow name parameter in the TF Placeholder.
* Implement neural_net_label_input
 * Return a TF Placeholder
 * Set the shape using n_classes with batch size set to None.
 * Name the TensorFlow placeholder "y" using the TensorFlow name parameter in the TF Placeholder.
* Implement neural_net_keep_prob_input
 * Return a TF Placeholder for dropout keep probability.
 * Name the TensorFlow placeholder "keep_prob" using the TensorFlow name parameter in...
Can you write Python code for it?

import tensorflow as tf

def neural_net_image_input(image_shape):
    """
    Return a Tensor for a batch of image input
    : image_shape: Shape of the images
    : return: Tensor for image input.
    """
    # TODO: Implement Function
    return tf.placeholder(tf.float32,shape=[None,image_shape[0],image_shape[1],image_shape[2]],name='x')

def neural_net_label_input(n_classes):
    """
    Return a Tensor for a batch of label input
    : n_classes: Number of classes
    : return: Tensor for label input.
    """
    # TODO: Implement Function
    return tf.placeholder(tf.float32,shape=(None,n_classes),name='y')

def neural_net_keep_prob_input():
    """
    Return a Tensor for keep probability
    : return: Tensor for keep probability.
    """
    # TODO: Implement Function
    return tf.placeholder(tf.float32,name='keep_prob')

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tf.reset_default_graph()
tests.test_nn_image_inputs(neural_net_image_input)
tests.test_nn_label_inputs(neural_net_label_input)
tests.test_nn_keep_prob_inputs(neural_net_keep_prob_input)


I am working on the file "Project_2_image_classification/dlnd_image_classification.ipynb".
The first task is:
Check Point
This is your first checkpoint.  If you ever decide to come back to this notebook or have to restart the notebook, you can start from here.  The preprocessed data has been saved to disk.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import pickle
import problem_unittests as tests
import helper

# Load the Preprocessed Validation data
valid_features, valid_labels = pickle.load(open('preprocess_validation.p', mode='rb'))


I am working on the file "Project_2_image_classification/dlnd_image_classification.ipynb".
The first task is:
Checkpoint
The model has been saved to disk.
Test Model
Test your model against the test dataset.  This will be your final accuracy. You should have an accuracy greater than 50%. If you don't, keep tweaking the model architecture and parameters.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import tensorflow as tf
import pickle
import helper
import random

# Set batch size if not already set
try:
    if batch_size:
        pass
except NameError:
    batch_size = 64

save_model_path = './image_classification'
n_samples = 4
top_n_predictions = 3

def test_model():
    """
    Test the saved model against the test dataset
    """

    test_features, test_labels = pickle.load(open('preprocess_training.p', mode='rb'))
    loaded_graph = tf.Graph()

    with tf.Session(graph=loaded_graph) as sess:
        # Load model
        loader = tf.train.import_meta_graph(save_model_path + '.meta')
        loader.restore(sess, save_model_path)

        # Get Tensors from loaded model
        loaded_x = loaded_graph.get_tensor_by_name('x:0')
        loaded_y = loaded_graph.get_tensor_by_name('y:0')
        loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0')
        loaded_logits = loaded_graph.get_tensor_by_name('logits:0')
        loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0')

        # Get accuracy in batches for memory limitations
        test_batch_acc_total = 0
        test_batch_count = 0

        for train_feature_batch, train_label_batch in helper.batch_features_labels(test_features, test_labels, batch_size):
            test_batch_acc_total += sess.run(
                loaded_acc,
                feed_dict={loaded_x: train_feature_batch, loaded_y: train_label_batch, loaded_keep_prob: 1.0})
            test_batch_count += 1

        print('Testing Accuracy: {}\n'.format(test_batch_acc_total/test_batch_count))

        # Print Random Samples
        random_test_features, random_test_labels = tuple(zip(*random.sample(list(zip(test_features, test_labels)), n_samples)))
        random_test_predictions = sess.run(
            tf.nn.top_k(tf.nn.softmax(loaded_logits), top_n_predictions),
            feed_dict={loaded_x: random_test_features, loaded_y:...


I am working on the file "Project_2_image_classification/dlnd_image_classification.ipynb".
The first task is:
Convolution and Max Pooling Layer
Convolution layers have a lot of success with images. For this code cell, you should implement the function conv2d_maxpool to apply convolution then max pooling:
* Create the weight and bias using conv_ksize, conv_num_outputs and the shape of x_tensor.
* Apply a convolution to x_tensor using weight and conv_strides.
 * We recommend you use same padding, but you're welcome to use any padding.
* Add bias
* Add a nonlinear activation to the convolution.
* Apply Max Pooling using pool_ksize and pool_strides.
 * We recommend you use same padding, but you're welcome to use any padding.
Note: You can't use TensorFlow Layers or TensorFlow Layers (contrib) for this layer, but you can still use TensorFlow's Neural Network package. You may still use the shortcut option for all the other layers.
Can you write Python code for it?

def conv2d_maxpool(x_tensor, conv_num_outputs, conv_ksize, conv_strides, pool_ksize, pool_strides):
    """
    Apply convolution then max pooling to x_tensor
    :param x_tensor: TensorFlow Tensor
    :param conv_num_outputs: Number of outputs for the convolutional layer
    :param conv_ksize: kernal size 2-D Tuple for the convolutional layer
    :param conv_strides: Stride 2-D Tuple for convolution
    :param pool_ksize: kernal size 2-D Tuple for pool
    :param pool_strides: Stride 2-D Tuple for pool
    : return: A tensor that represents convolution and max pooling of x_tensor
    """
    # TODO: Implement Function
    input_list = x_tensor.get_shape().as_list()
    input_channels = input_list[3]
    input_height = input_list[1]
    input_width = input_list[2]
    w  = tf.Variable(tf.random_normal([conv_ksize[0],conv_ksize[1],input_channels,conv_num_outputs],stddev=0.1))
    b = tf.Variable(tf.zeros(shape=[conv_num_outputs]))
    x_tensor = tf.nn.bias_add(tf.nn.conv2d(x_tensor,w,[1,conv_strides[0],conv_strides[1],1],padding='SAME'),b)
    x_tensor = tf.nn.relu(x_tensor)
    x_tensor = tf.nn.max_pool(x_tensor,[1,pool_ksize[0],pool_ksize[1],1],[1,pool_strides[0],pool_strides[1],1],padding='SAME')
    return x_tensor

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_con_pool(conv2d_maxpool)


I am working on the file "Project_2_image_classification/dlnd_image_classification.ipynb".
The first task is:
Create Convolutional Model
Implement the function conv_net to create a convolutional neural network model. The function takes in a batch of images, x, and outputs logits.  Use the layers you created above to create this model:

Apply 1, 2, or 3 Convolution and Max Pool layers
Apply a Flatten Layer
Apply 1, 2, or 3 Fully Connected Layers
Apply an Output Layer
Return the output
Apply TensorFlow's Dropout to one or more layers in the model using keep_prob.
Can you write Python code for it?

def conv_net(x, keep_prob):
    """
    Create a convolutional neural network model
    : x: Placeholder tensor that holds image data.
    : keep_prob: Placeholder tensor that hold dropout keep probability.
    : return: Tensor that represents logits
    """
    # TODO: Apply 1, 2, or 3 Convolution and Max Pool layers
    #    Play around with different number of outputs, kernel size and stride
    # Function Definition from Above:
    x_tensor = x
    #x_tensor = conv2d_maxpool(x_tensor, conv_num_outputs, conv_ksize, conv_strides, pool_ksize, pool_strides)
    x_tensor = conv2d_maxpool(x_tensor,  16,               (5,5),      (1,1),        (2,2),      (2,2))
    x_tensor = conv2d_maxpool(x_tensor,  20,               (5,5),      (1,1),        (2,2),      (2,2))
    x_tensor = conv2d_maxpool(x_tensor,  20,               (5,5),      (1,1),        (2,2),      (2,2))
    # TODO: Apply a Flatten Layer
    # Function Definition from Above:
    x_tensor = flatten(x_tensor)

    # TODO: Apply 1, 2, or 3 Fully Connected Layers
    #    Play around with different number of outputs
    # Function Definition from Above:
    ##--1--##
    num_outputs_f_1 = 256
    x_tensor = fully_conn(x_tensor, num_outputs_f_1)
    x_tensor = tf.nn.dropout(x_tensor,keep_prob)
#    ##--2--##
#    num_outputs_f_2 = 2048
#    x_tensor = fully_conn(x_tensor, num_outputs_f_2)
#    x_tensor = tf.nn.relu(x_tensor)
#    x_tensor = tf.nn.dropout(x_tensor,keep_prob)
#    ##--3--##
#    num_outputs_f_3 = 1024
#    x_tensor = fully_conn(x_tensor, num_outputs_f_3)
#    x_tensor = tf.nn.relu(x_tensor)
#    x_tensor = tf.nn.dropout(x_tensor,keep_prob)

    # TODO: Apply an Output Layer
    #    Set this to the number of classes
    # Function Definition from Above:
    num_outputs_o = 10
    x_tensor = output(x_tensor, num_outputs_o)

    # TODO: return output
    return x_tensor

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""

##############################
## Build the Neural Network...


I am working on the file "Project_2_image_classification/dlnd_image_classification.ipynb".
The first task is:
Explore the Data
The dataset is broken into batches to prevent your machine from running out of memory.  The CIFAR-10 dataset consists of 5 batches, named data_batch_1, data_batch_2, etc.. Each batch contains the labels and images that are one of the following:
* airplane
* automobile
* bird
* cat
* deer
* dog
* frog
* horse
* ship
* truck
Understanding a dataset is part of making predictions on the data.  Play around with the code cell below by changing the batch_id and sample_id. The batch_id is the id for a batch (1-5). The sample_id is the id for a image and label pair in the batch.
Ask yourself "What are all possible labels?", "What is the range of values for the image data?", "Are the labels in order or random?".  Answers to questions like these will help you preprocess the data and end up with better predictions.
Can you write Python code for it?

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import helper
import numpy as np

# Explore the dataset
batch_id = 1
sample_id = 11
helper.display_stats(cifar10_dataset_folder_path, batch_id, sample_id)