Skip to content

Commit

Permalink
model training walkthrough minor improvements for clarity (tensorflow…
Browse files Browse the repository at this point in the history
…#516)

contentsOfCSV -> contentsOf. CSV is already in the function name so this
arg name seems needlessly verbose.

Use local variable names in comments about "This should never happen".
Saying "we construct <param name>" was a bit confusing to me, especially
since the param name in this case is also the name of the python module.

Print tensor shapes. I found this helpful to understand the process.

Co-authored-by: Saleem Abdulrasool <[email protected]>
  • Loading branch information
Gary Miguel and compnerd authored Aug 19, 2020
1 parent 183ca60 commit ca708f3
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions docs/site/tutorials/model_training_walkthrough.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -395,29 +395,29 @@
"source": [
"/// Initialize an `IrisBatch` dataset from a CSV file.\n",
"func loadIrisDatasetFromCSV(\n",
" contentsOfCSVFile: String, hasHeader: Bool, featureColumns: [Int], labelColumns: [Int]) -> [IrisBatch] {\n",
" contentsOf: String, hasHeader: Bool, featureColumns: [Int], labelColumns: [Int]) -> [IrisBatch] {\n",
" let np = Python.import(\"numpy\")\n",
"\n",
" let featuresNp = np.loadtxt(\n",
" contentsOfCSVFile,\n",
" contentsOf,\n",
" delimiter: \",\",\n",
" skiprows: hasHeader ? 1 : 0,\n",
" usecols: featureColumns,\n",
" dtype: Float.numpyScalarTypes.first!)\n",
" guard let featuresTensor = Tensor<Float>(numpy: featuresNp) else {\n",
" // This should never happen, because we construct numpy in such a\n",
" // This should never happen, because we construct featuresNp in such a\n",
" // way that it should be convertible to tensor.\n",
" fatalError(\"np.loadtxt result can't be converted to Tensor\")\n",
" }\n",
"\n",
" let labelsNp = np.loadtxt(\n",
" contentsOfCSVFile,\n",
" contentsOf,\n",
" delimiter: \",\",\n",
" skiprows: hasHeader ? 1 : 0,\n",
" usecols: labelColumns,\n",
" dtype: Int32.numpyScalarTypes.first!)\n",
" guard let labelsTensor = Tensor<Int32>(numpy: labelsNp) else {\n",
" // This should never happen, because we construct numpy in such a\n",
" // This should never happen, because we construct labelsNp in such a\n",
" // way that it should be convertible to tensor.\n",
" fatalError(\"np.loadtxt result can't be converted to Tensor\")\n",
" }\n",
Expand All @@ -440,7 +440,7 @@
"metadata": {},
"outputs": [],
"source": [
"let trainingDataset: [IrisBatch] = loadIrisDatasetFromCSV(contentsOfCSVFile: trainDataFilename, \n",
"let trainingDataset: [IrisBatch] = loadIrisDatasetFromCSV(contentsOf: trainDataFilename, \n",
" hasHeader: true, \n",
" featureColumns: [0, 1, 2, 3], \n",
" labelColumns: [4])\n",
Expand Down Expand Up @@ -479,7 +479,9 @@
"let firstTrainLabels = firstTrainBatch.labels\n",
"\n",
"print(\"First batch of features: \\(firstTrainFeatures)\")\n",
"print(\"First batch of labels: \\(firstTrainLabels)\")"
"print(\"firstTrainFeatures.shape: \\(firstTrainFeatures.shape)\")\n",
"print(\"First batch of labels: \\(firstTrainLabels)\")\n",
"print(\"firstTrainLabels.shape: \\(firstTrainLabels.shape)\")"
]
},
{
Expand Down Expand Up @@ -1110,7 +1112,7 @@
"outputs": [],
"source": [
"let testDataset = loadIrisDatasetFromCSV(\n",
" contentsOfCSVFile: testDataFilename, hasHeader: true,\n",
" contentsOf: testDataFilename, hasHeader: true,\n",
" featureColumns: [0, 1, 2, 3], labelColumns: [4]).inBatches(of: batchSize)"
]
},
Expand Down

0 comments on commit ca708f3

Please sign in to comment.